合并development代码到master #1
|
@ -6,6 +6,7 @@ import com.dite.znpt.domain.page.PageBean;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@ -13,17 +14,28 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
@Service
|
@Service
|
||||||
public interface BusinessDataFileService {
|
public interface BusinessDataFileService {
|
||||||
|
|
||||||
|
@ApiOperation("分页查询文件")
|
||||||
PageBean pageSelect(Integer page, Integer pageSize, Long folderId, String fileName);
|
PageBean pageSelect(Integer page, Integer pageSize, Long folderId, String fileName);
|
||||||
|
|
||||||
|
@ApiOperation("删除文件")
|
||||||
Result delete(@RequestParam(value = "fileId", required = false) Long fileId,@RequestParam(value = "foldelId", required = false) Long folderId);
|
Result delete(@RequestParam(value = "fileId", required = false) Long fileId,@RequestParam(value = "foldelId", required = false) Long folderId);
|
||||||
|
|
||||||
|
@ApiOperation("增加数据库文件信息")
|
||||||
void add(BusinessDataFileEntity businessDataFileEntity);
|
void add(BusinessDataFileEntity businessDataFileEntity);
|
||||||
|
|
||||||
|
@ApiOperation("获取文件路径")
|
||||||
String getPath(Long fileId);
|
String getPath(Long fileId);
|
||||||
|
|
||||||
|
@ApiOperation("删除文件")
|
||||||
// 在接口中添加重命名方法
|
// 在接口中添加重命名方法
|
||||||
Result reName(Long fileId, String newFileName);
|
Result reName(Long fileId, String newFileName);
|
||||||
|
|
||||||
|
@ApiOperation("下载文件")
|
||||||
|
void upload(Long fileId, HttpServletResponse response);
|
||||||
|
|
||||||
|
@ApiOperation("上传文件")
|
||||||
|
Result addfile(MultipartFile file, Long folderId);
|
||||||
|
|
||||||
// 在接口中添加预览方法
|
// 在接口中添加预览方法
|
||||||
// Result preview(Long fileId, HttpServletResponse response);
|
// Result preview(Long fileId, HttpServletResponse response);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import com.dite.znpt.domain.Result;
|
||||||
import com.dite.znpt.domain.page.PageBean;
|
import com.dite.znpt.domain.page.PageBean;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
@ApiOperation("商务资料文件夹service")
|
@ApiOperation("商务资料文件夹service")
|
||||||
public interface BusinessDataService {
|
public interface BusinessDataService {
|
||||||
|
|
||||||
|
@ -12,4 +14,5 @@ public interface BusinessDataService {
|
||||||
String getPath(Long parentId);
|
String getPath(Long parentId);
|
||||||
Result delete(Long folderId);
|
Result delete(Long folderId);
|
||||||
Result reName(Long folderId, String newName);
|
Result reName(Long folderId, String newName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,20 +2,26 @@ package com.dite.znpt.service.impl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.dite.znpt.domain.Result;
|
import com.dite.znpt.domain.Result;
|
||||||
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
|
import com.dite.znpt.domain.entity.BusinessDataFileEntity;
|
||||||
import com.dite.znpt.domain.page.PageBean;
|
import com.dite.znpt.domain.page.PageBean;
|
||||||
import com.dite.znpt.mapper.BusinessDataFileMapper;
|
import com.dite.znpt.mapper.BusinessDataFileMapper;
|
||||||
|
import com.dite.znpt.mapper.BusinessDataMapper;
|
||||||
import com.dite.znpt.service.BusinessDataFileService;
|
import com.dite.znpt.service.BusinessDataFileService;
|
||||||
|
import com.dite.znpt.service.BusinessDataService;
|
||||||
import com.github.pagehelper.Page;
|
import com.github.pagehelper.Page;
|
||||||
import com.github.pagehelper.PageHelper;
|
import com.github.pagehelper.PageHelper;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.File;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static jodd.io.FileUtil.deleteFile;
|
import static jodd.io.FileUtil.deleteFile;
|
||||||
|
@ -27,6 +33,8 @@ import static org.apache.tomcat.util.http.fileupload.FileUtils.deleteDirectory;
|
||||||
public class BusinessDataFileServiceImpl implements BusinessDataFileService {
|
public class BusinessDataFileServiceImpl implements BusinessDataFileService {
|
||||||
@Resource
|
@Resource
|
||||||
private BusinessDataFileMapper businessDataFileMapper;
|
private BusinessDataFileMapper businessDataFileMapper;
|
||||||
|
@Resource
|
||||||
|
private BusinessDataService businessDataService;
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation("分页查询文件")
|
@ApiOperation("分页查询文件")
|
||||||
|
@ -156,6 +164,79 @@ public class BusinessDataFileServiceImpl implements BusinessDataFileService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("下载文件")
|
||||||
|
@Override
|
||||||
|
public void upload(Long fileId, HttpServletResponse response) {
|
||||||
|
String path = getPath(fileId);
|
||||||
|
try {
|
||||||
|
// path是指想要下载的文件的路径
|
||||||
|
File file = new File(path);
|
||||||
|
// 获取文件名
|
||||||
|
String filename = file.getName();
|
||||||
|
// 获取文件后缀名
|
||||||
|
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
||||||
|
// 将文件写入输入流
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(file);
|
||||||
|
InputStream fis = new BufferedInputStream(fileInputStream);
|
||||||
|
byte[] buffer = new byte[fis.available()];
|
||||||
|
fis.read(buffer);
|
||||||
|
fis.close();
|
||||||
|
|
||||||
|
// 清空response
|
||||||
|
response.reset();
|
||||||
|
// 设置response的Header
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
||||||
|
// 告知浏览器文件的大小
|
||||||
|
response.addHeader("Content-Length", "" + file.length());
|
||||||
|
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
outputStream.write(buffer);
|
||||||
|
outputStream.flush();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation("上传文件")
|
||||||
|
@Override
|
||||||
|
public Result addfile(MultipartFile file, Long folderId) {
|
||||||
|
String loginIdAsLong = "未登录用户";
|
||||||
|
if(StpUtil.isLogin()){
|
||||||
|
loginIdAsLong = StpUtil.getLoginIdAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.isEmpty()) {
|
||||||
|
return Result.error("上传文件为空");
|
||||||
|
}
|
||||||
|
// TODO 以后可以优化,就算文件名一样,加个(1),(2)这种
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] bytes = file.getBytes();
|
||||||
|
String uploadDir = businessDataService.getPath(folderId);
|
||||||
|
|
||||||
|
File uploadedFile = new File(uploadDir + "\\" + file.getOriginalFilename());
|
||||||
|
if (uploadedFile.exists()) {
|
||||||
|
return Result.error("文件已存在");
|
||||||
|
}
|
||||||
|
file.transferTo(uploadedFile);
|
||||||
|
|
||||||
|
// 保存文件信息到数据
|
||||||
|
BusinessDataFileEntity fileEntity = new BusinessDataFileEntity();
|
||||||
|
fileEntity.setFolderId(folderId);
|
||||||
|
fileEntity.setFileName(file.getOriginalFilename());
|
||||||
|
fileEntity.setFilePath(uploadDir + "\\" + file.getOriginalFilename());
|
||||||
|
fileEntity.setFileType(file.getContentType());
|
||||||
|
fileEntity.setFileSize(file.getSize()/1024);
|
||||||
|
fileEntity.setUploadTime(new Date());
|
||||||
|
fileEntity.setUploaderId(loginIdAsLong);
|
||||||
|
System.out.println(uploadDir + "\\" + file.getOriginalFilename());
|
||||||
|
add(fileEntity);
|
||||||
|
|
||||||
|
return Result.okM("上传成功");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return Result.error("上传失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,10 @@ import com.dite.znpt.domain.entity.BusinessDataFileEntity;
|
||||||
import com.dite.znpt.domain.page.PageBean;
|
import com.dite.znpt.domain.page.PageBean;
|
||||||
import com.dite.znpt.mapper.BusinessDataFileMapper;
|
import com.dite.znpt.mapper.BusinessDataFileMapper;
|
||||||
import com.dite.znpt.mapper.BusinessDataMapper;
|
import com.dite.znpt.mapper.BusinessDataMapper;
|
||||||
|
|
||||||
import com.dite.znpt.service.BusinessDataFileService;
|
import com.dite.znpt.service.BusinessDataFileService;
|
||||||
import com.dite.znpt.service.BusinessDataService;
|
import com.dite.znpt.service.BusinessDataService;
|
||||||
|
import com.dite.znpt.service.impl.BusinessDataFileServiceImpl;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -54,43 +56,7 @@ public class BusinessDataFileController {
|
||||||
@PostMapping("/add")
|
@PostMapping("/add")
|
||||||
public Result add(@RequestParam("file") MultipartFile file,
|
public Result add(@RequestParam("file") MultipartFile file,
|
||||||
@RequestParam Long folderId) {
|
@RequestParam Long folderId) {
|
||||||
String loginIdAsLong = "未登录用户";
|
return businessDataFileService.addfile(file, folderId);
|
||||||
if(StpUtil.isLogin()){
|
|
||||||
loginIdAsLong = StpUtil.getLoginIdAsString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file.isEmpty()) {
|
|
||||||
return Result.error("上传文件为空");
|
|
||||||
}
|
|
||||||
// TODO 以后可以优化,就算文件名一样,加个(1),(2)这种
|
|
||||||
|
|
||||||
try {
|
|
||||||
byte[] bytes = file.getBytes();
|
|
||||||
String uploadDir = businessDataService.getPath(folderId);
|
|
||||||
|
|
||||||
File uploadedFile = new File(uploadDir + "\\" + file.getOriginalFilename());
|
|
||||||
if (uploadedFile.exists()) {
|
|
||||||
return Result.error("文件已存在");
|
|
||||||
}
|
|
||||||
file.transferTo(uploadedFile);
|
|
||||||
|
|
||||||
// 保存文件信息到数据
|
|
||||||
BusinessDataFileEntity fileEntity = new BusinessDataFileEntity();
|
|
||||||
fileEntity.setFolderId(folderId);
|
|
||||||
fileEntity.setFileName(file.getOriginalFilename());
|
|
||||||
fileEntity.setFilePath(uploadDir + "\\" + file.getOriginalFilename());
|
|
||||||
fileEntity.setFileType(file.getContentType());
|
|
||||||
fileEntity.setFileSize(file.getSize()/1024);
|
|
||||||
fileEntity.setUploadTime(new Date());
|
|
||||||
fileEntity.setUploaderId(loginIdAsLong);
|
|
||||||
System.out.println(uploadDir + "\\" + file.getOriginalFilename());
|
|
||||||
businessDataFileService.add(fileEntity);
|
|
||||||
|
|
||||||
return Result.okM("上传成功");
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return Result.error("上传失败");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "删除文件")
|
@ApiOperation(value = "删除文件")
|
||||||
|
@ -103,38 +69,7 @@ public class BusinessDataFileController {
|
||||||
@ApiOperation(value = "下载文件")
|
@ApiOperation(value = "下载文件")
|
||||||
@GetMapping("/download")
|
@GetMapping("/download")
|
||||||
public void download(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
|
public void download(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
|
||||||
String path = businessDataFileService.getPath(fileId);
|
businessDataFileService.upload(fileId, response);
|
||||||
try {
|
|
||||||
// path是指想要下载的文件的路径
|
|
||||||
File file = new File(path);
|
|
||||||
log.info(file.getPath());
|
|
||||||
// 获取文件名
|
|
||||||
String filename = file.getName();
|
|
||||||
// 获取文件后缀名
|
|
||||||
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
|
|
||||||
log.info("文件后缀名:" + ext);
|
|
||||||
|
|
||||||
// 将文件写入输入流
|
|
||||||
FileInputStream fileInputStream = new FileInputStream(file);
|
|
||||||
InputStream fis = new BufferedInputStream(fileInputStream);
|
|
||||||
byte[] buffer = new byte[fis.available()];
|
|
||||||
fis.read(buffer);
|
|
||||||
fis.close();
|
|
||||||
|
|
||||||
// 清空response
|
|
||||||
response.reset();
|
|
||||||
// 设置response的Header
|
|
||||||
response.setCharacterEncoding("UTF-8");
|
|
||||||
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
|
||||||
// 告知浏览器文件的大小
|
|
||||||
response.addHeader("Content-Length", "" + file.length());
|
|
||||||
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
|
|
||||||
response.setContentType("application/octet-stream");
|
|
||||||
outputStream.write(buffer);
|
|
||||||
outputStream.flush();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation(value = "重命名文件", httpMethod = "PUT")
|
@ApiOperation(value = "重命名文件", httpMethod = "PUT")
|
||||||
|
|
Loading…
Reference in New Issue