合并development代码到master #1
|
@ -6,6 +6,7 @@ import com.dite.znpt.domain.page.PageBean;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -13,17 +14,28 @@ import javax.servlet.http.HttpServletResponse;
|
|||
@Service
|
||||
public interface BusinessDataFileService {
|
||||
|
||||
@ApiOperation("分页查询文件")
|
||||
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);
|
||||
|
||||
@ApiOperation("增加数据库文件信息")
|
||||
void add(BusinessDataFileEntity businessDataFileEntity);
|
||||
|
||||
@ApiOperation("获取文件路径")
|
||||
String getPath(Long fileId);
|
||||
|
||||
@ApiOperation("删除文件")
|
||||
// 在接口中添加重命名方法
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import com.dite.znpt.domain.Result;
|
|||
import com.dite.znpt.domain.page.PageBean;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@ApiOperation("商务资料文件夹service")
|
||||
public interface BusinessDataService {
|
||||
|
||||
|
@ -12,4 +14,5 @@ public interface BusinessDataService {
|
|||
String getPath(Long parentId);
|
||||
Result delete(Long folderId);
|
||||
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.entity.BusinessDataFileEntity;
|
||||
import com.dite.znpt.domain.page.PageBean;
|
||||
import com.dite.znpt.mapper.BusinessDataFileMapper;
|
||||
import com.dite.znpt.mapper.BusinessDataMapper;
|
||||
import com.dite.znpt.service.BusinessDataFileService;
|
||||
import com.dite.znpt.service.BusinessDataService;
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
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 {
|
||||
@Resource
|
||||
private BusinessDataFileMapper businessDataFileMapper;
|
||||
@Resource
|
||||
private BusinessDataService businessDataService;
|
||||
|
||||
|
||||
@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.mapper.BusinessDataFileMapper;
|
||||
import com.dite.znpt.mapper.BusinessDataMapper;
|
||||
|
||||
import com.dite.znpt.service.BusinessDataFileService;
|
||||
import com.dite.znpt.service.BusinessDataService;
|
||||
import com.dite.znpt.service.impl.BusinessDataFileServiceImpl;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -54,43 +56,7 @@ public class BusinessDataFileController {
|
|||
@PostMapping("/add")
|
||||
public Result add(@RequestParam("file") MultipartFile file,
|
||||
@RequestParam 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());
|
||||
businessDataFileService.add(fileEntity);
|
||||
|
||||
return Result.okM("上传成功");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("上传失败");
|
||||
}
|
||||
return businessDataFileService.addfile(file, folderId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除文件")
|
||||
|
@ -103,38 +69,7 @@ public class BusinessDataFileController {
|
|||
@ApiOperation(value = "下载文件")
|
||||
@GetMapping("/download")
|
||||
public void download(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
|
||||
String path = businessDataFileService.getPath(fileId);
|
||||
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();
|
||||
}
|
||||
businessDataFileService.upload(fileId, response);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "重命名文件", httpMethod = "PUT")
|
||||
|
|
Loading…
Reference in New Issue