提交 6cab523d authored 作者: zhangshuang's avatar zhangshuang

文件信息保存数据库与swagger

上级 00b27522
...@@ -18,7 +18,7 @@ import static com.zjty.efs.ftp.base.response.ResponseCode.*; ...@@ -18,7 +18,7 @@ import static com.zjty.efs.ftp.base.response.ResponseCode.*;
@SuppressWarnings({"WeakerAccess", "unused"}) @SuppressWarnings({"WeakerAccess", "unused"})
@Getter @Getter
@AutoDocument @AutoDocument
@ApiModel(value = "响应体", description = "封装好的响应体") @ApiModel(value = "ServerResponse", description = "封装好的响应体")
public class ServerResponse<T> { public class ServerResponse<T> {
/** /**
* 错误码 * 错误码
......
...@@ -27,10 +27,10 @@ public class FileUploadController { ...@@ -27,10 +27,10 @@ public class FileUploadController {
private FileUploadService fileUploadService; private FileUploadService fileUploadService;
//文件上传 //文件上传
@PutMapping("/upload") /*@PutMapping("/upload")
public ServerResponse fileUpload(HttpServletRequest httpServletRequest){ public ServerResponse fileUpload(HttpServletRequest httpServletRequest){
return fileUploadService.fileUpload(httpServletRequest); return fileUploadService.fileUpload(httpServletRequest);
} }*/
/* *//** /* *//**
* @author zs * @author zs
...@@ -53,7 +53,7 @@ public class FileUploadController { ...@@ -53,7 +53,7 @@ public class FileUploadController {
@PostMapping("/checkChunk") @PostMapping("/checkChunk")
@ApiOperation(value = "分片检查",notes = "检查分片存不存在") @ApiOperation(value = "分片检查",notes = "检查分片存不存在")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "checkChunkRequests",value = "检查分片集合存不存在",paramType = "query",required = true) @ApiImplicitParam(name = "checkChunkRequests",value = "检查分片集合存不存在",dataType = "List",paramType = "query",required = true)
}) })
public ServerResponse checkChunk(@RequestBody List<CheckChunkRequest> checkChunkRequests) { public ServerResponse checkChunk(@RequestBody List<CheckChunkRequest> checkChunkRequests) {
return fileUploadService.checkChunk(checkChunkRequests); return fileUploadService.checkChunk(checkChunkRequests);
...@@ -66,10 +66,15 @@ public class FileUploadController { ...@@ -66,10 +66,15 @@ public class FileUploadController {
@PostMapping("/upload") @PostMapping("/upload")
@ApiOperation(value = "文件上传",notes = "上传文件,支持断点续传") @ApiOperation(value = "文件上传",notes = "上传文件,支持断点续传")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "uploadRequests",value = "上传文件的集合",paramType = "query",required = true) @ApiImplicitParam(name = "file",value = "文件",dataType = "file",paramType = "query",required = true),
@ApiImplicitParam(name = "md5File",value = "MD5生成的文件名",dataType = "String",paramType = "query",required = true),
@ApiImplicitParam(name = "chunk",value = "分片",dataType = "String",paramType = "query")
}) })
public ServerResponse upload(@RequestBody List<UploadRequest> uploadRequests) { //第几片,从0开始 @ResponseBody
return fileUploadService.upload(uploadRequests); public ServerResponse upload(@RequestParam(value = "file") MultipartFile file,
@RequestParam(value = "md5File") String md5File,
@RequestParam(value = "chunk",required= false) Integer chunk) { //第几片,从0开始
return fileUploadService.upload(file,md5File,chunk);
} }
/** /**
...@@ -79,7 +84,7 @@ public class FileUploadController { ...@@ -79,7 +84,7 @@ public class FileUploadController {
@PostMapping("/merge") @PostMapping("/merge")
@ApiOperation(value = "合成分片",notes = "合成所有分片") @ApiOperation(value = "合成分片",notes = "合成所有分片")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "mergeRequests",value = "需要分片合成的集合",paramType = "query",required = true) @ApiImplicitParam(name = "mergeRequests",value = "需要分片合成的集合",dataType = "list",paramType = "query",required = true)
}) })
public ServerResponse merge(@RequestBody List<MergeRequest> mergeRequests){ public ServerResponse merge(@RequestBody List<MergeRequest> mergeRequests){
return fileUploadService.merge(mergeRequests); return fileUploadService.merge(mergeRequests);
......
...@@ -11,11 +11,11 @@ import lombok.NoArgsConstructor; ...@@ -11,11 +11,11 @@ import lombok.NoArgsConstructor;
@AutoDocument @AutoDocument
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ApiModel(value = "分片检查",description = "分片检查请求体") @ApiModel(value = "CheckChunkRequest",description = "分片检查请求体")
@Data @Data
public class CheckChunkRequest { public class CheckChunkRequest {
@ApiModelProperty(value = "md5File",notes = "md5生成的文件夹名称") @ApiModelProperty(name = "md5File",value = "md5生成的文件夹名称",notes = "md5生成的文件夹名称")
private String md5File; private String md5File;
@ApiModelProperty(value = "chunk",notes = "当前分片") @ApiModelProperty(name = "chunk",value = "当前分片",notes = "当前分片")
private Integer chunk; private Integer chunk;
} }
...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor; ...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ApiModel(value = "分片检查",description = "分片检查响应体") @ApiModel(value = "CheckChunkResponse",description = "分片检查响应体")
public class CheckChunkResponse { public class CheckChunkResponse {
@ApiModelProperty(value = "md5File",notes = "md5生成的文件夹名称") @ApiModelProperty(name = "md5File",value = "md5生成的文件夹名称",notes = "md5生成的文件夹名称")
private String md5File; private String md5File;
@ApiModelProperty(value = "chunk",notes = "当前分片") @ApiModelProperty(name = "chunk",value = "当前分片",notes = "当前分片")
private Integer chunk; private Integer chunk;
@ApiModelProperty(value = "exist",notes = "是否存在 0-不存在 1-存在") @ApiModelProperty(name = "exist",value = "是否存在 0-不存在 1-存在",notes = "是否存在 0-不存在 1-存在")
private Integer exist; private Integer exist;
} }
...@@ -18,16 +18,16 @@ import javax.persistence.Id; ...@@ -18,16 +18,16 @@ import javax.persistence.Id;
@NoArgsConstructor @NoArgsConstructor
@Data @Data
@Entity @Entity
@ApiModel(value = "文件存储",description = "将文件信息存储在数据库中") @ApiModel(value = "FileSave",description = "将文件信息存储在数据库中")
public class FileSave { public class FileSave {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@ApiModelProperty(value = "id",notes = "文件id",example = "1") @ApiModelProperty(name = "id",value = "文件id",notes = "文件id",example = "1")
private Integer id;//文件id private Integer id;//文件id
@ApiModelProperty(value = "realName",notes = "文件真正存储名称",example = "1(1).zip") @ApiModelProperty(name = "realName",value = "文件真正存储名称",notes = "文件真正存储名称",example = "1(1).zip")
private String realName;//真实名称 private String realName;//真实名称
@ApiModelProperty(value = "fileName",notes = "文件传入名称",example = "1.zip") @ApiModelProperty(name = "fileName",value = "文件传入名称",notes = "文件传入名称",example = "1.zip")
private String fileName;//传入名称 private String fileName;//传入名称
@ApiModelProperty(value = "path",notes = "文件真正存储路径",example = "/file/uploads/1(1).zip") @ApiModelProperty(name = "path",value = "文件真正存储路径",notes = "文件真正存储路径",example = "/file/uploads/1(1).zip")
private String path;//文件存储路径 private String path;//文件存储路径
} }
...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor; ...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Data @Data
@ApiModel(value = "合并",description = "合并请求体") @ApiModel(value = "MergeRequest",description = "合并请求体")
public class MergeRequest { public class MergeRequest {
@ApiModelProperty(value = "name",notes = "合成后的文件名称") @ApiModelProperty(name = "name",value = "合成后的文件名称",notes = "合成后的文件名称",example = "1.zip")
private String name;//文件 private String name;//文件
@ApiModelProperty(value = "md5File",notes = "MD5生成的文件夹名称",example = "fc44454f994403b01b40895f0a6535b5") @ApiModelProperty(name = "md5File",value = "MD5生成的文件夹名称",notes = "MD5生成的文件夹名称",example = "fc44454f994403b01b40895f0a6535b5")
private String md5File;//md5文件 private String md5File;//md5文件
@ApiModelProperty(value = "chuck",notes = "当前分片",example = "1") @ApiModelProperty(name = "chuck",value = "当前分片",notes = "当前分片",example = "1")
private Integer chuck;//分片 private Integer chuck;//分片
} }
...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor; ...@@ -12,12 +12,12 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Data @Data
@ApiModel(value = "合并",description = "合并响应体") @ApiModel(value = "MergeResponse",description = "合并响应体")
public class MergeResponse { public class MergeResponse {
@ApiModelProperty(value = "name",notes = "上传文件名称",example = "1.zip") @ApiModelProperty(name = "name",value = "上传文件名称",notes = "上传文件名称",example = "1.zip")
private String name;//上传文件的名称 private String name;//上传文件的名称
@ApiModelProperty(value = "path",notes = "上传文件服务器中存储地址",example = "D://file/uploads/1.zip") @ApiModelProperty(name = "path",value = "上传文件服务器中存储地址",notes = "上传文件服务器中存储地址",example = "D://file/uploads/1.zip")
private String path;//上传文件的地址 private String path;//上传文件的地址
@ApiModelProperty(value = "saveSuccess",notes = "文件保存是否成功 0-失败 1-成功",example = "1") @ApiModelProperty(name = "saveSuccess",value = "文件保存是否成功",notes = "文件保存是否成功 0-失败 1-成功",example = "1")
private int saveSuccess;//文件保存是否成功 0-失败 1-成功 private int saveSuccess;//文件保存是否成功 0-失败 1-成功
} }
package com.zjty.efs.ftp.entity; package com.zjty.efs.ftp.entity;
import com.zjty.efs.misc.config.AutoDocument; import com.zjty.efs.misc.config.AutoDocument;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -13,12 +12,12 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -13,12 +12,12 @@ import org.springframework.web.multipart.MultipartFile;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Data @Data
@ApiModel(value = "文件上传",description = "文件上传请求体") @ApiModel(value = "UploadRequest",description = "文件上传请求体")
public class UploadRequest { public class UploadRequest {
@ApiModelProperty(value = "multipartFile",notes = "文件数据") @ApiModelProperty(name = "multipartFile",value = "文件数据",notes = "文件数据")
private MultipartFile multipartFile;//文件 private MultipartFile multipartFile;//文件
@ApiModelProperty(value = "md5File",notes = "MD5生成的文件夹名称",example = "fc44454f994403b01b40895f0a6535b5") @ApiModelProperty(name = "md5File",value = "MD5生成的文件夹名称",notes = "MD5生成的文件夹名称",example = "fc44454f994403b01b40895f0a6535b5")
private String md5File;//md5文件 private String md5File;//md5文件
@ApiModelProperty(value = "chuck",notes = "当前分片",example = "1") @ApiModelProperty(name = "chuck",value = "当前分片",notes = "当前分片",example = "1")
private Integer chuck;//分片 private Integer chuck;//分片
} }
...@@ -35,7 +35,7 @@ public interface FileUploadService { ...@@ -35,7 +35,7 @@ public interface FileUploadService {
* @author zs * @author zs
* 修改上传 * 修改上传
*/ */
ServerResponse upload(List<UploadRequest> uploadRequests); ServerResponse upload(MultipartFile file, String md5File, Integer chunk);
/** /**
* @author zs * @author zs
......
...@@ -28,7 +28,7 @@ public class FileDownLoadServiceImpl implements FileDownLoadService { ...@@ -28,7 +28,7 @@ public class FileDownLoadServiceImpl implements FileDownLoadService {
* @param response * @param response
* 响应对象 * 响应对象
* @param fileName * @param fileName
* 文件名称(绝对) * 文件名称
*/ */
public void fileDownLoad(String fileName, HttpServletResponse response, HttpServletRequest httpServletRequest) { public void fileDownLoad(String fileName, HttpServletResponse response, HttpServletRequest httpServletRequest) {
if(fileName != null){ if(fileName != null){
...@@ -39,13 +39,12 @@ public class FileDownLoadServiceImpl implements FileDownLoadService { ...@@ -39,13 +39,12 @@ public class FileDownLoadServiceImpl implements FileDownLoadService {
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
} }
File file = new File(fileAddress + "/" + fileName); String path = fileAddress + "/" + fileName;
File file = new File(path);
long fileLength = file.length();
response.setHeader("Content-Length", fileLength + "");
//如果文件存在,返回文件,文件不存在,返回文件不存在提示 //如果文件存在,返回文件,文件不存在,返回文件不存在提示
boolean fileExist = false; if(fileLength != 0 ){
if(file.length() != 0){
fileExist = true;
}
if(fileExist){
setFileDownloadHeader(httpServletRequest,response,fileName); setFileDownloadHeader(httpServletRequest,response,fileName);
OutputStream os = null; OutputStream os = null;
InputStream is = null; InputStream is = null;
...@@ -62,8 +61,10 @@ public class FileDownLoadServiceImpl implements FileDownLoadService { ...@@ -62,8 +61,10 @@ public class FileDownLoadServiceImpl implements FileDownLoadService {
} }
os.flush(); os.flush();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
log.error(file.getAbsolutePath() + "文件不存在");
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
log.error(file.getAbsolutePath() + "文件读取异常");
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
...@@ -71,12 +72,15 @@ public class FileDownLoadServiceImpl implements FileDownLoadService { ...@@ -71,12 +72,15 @@ public class FileDownLoadServiceImpl implements FileDownLoadService {
is.close(); is.close();
os.close(); os.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.error("用户终止下载");
//e.printStackTrace();
} }
} }
}else { }else {
log.info("该文件不存在"); log.info(path + "该文件不存在");
} }
}else {
log.error("请求的文件名为空");
} }
} }
......
...@@ -122,12 +122,7 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -122,12 +122,7 @@ public class FileUploadServiceImpl implements FileUploadService {
} }
@Override @Override
public ServerResponse upload(List<UploadRequest> uploadRequests) { public ServerResponse upload(MultipartFile file, String md5File, Integer chunk) {
if(uploadRequests != null && uploadRequests.size() != 0){
for(UploadRequest uploadRequest:uploadRequests){
String md5File = uploadRequest.getMd5File();
Integer chunk = uploadRequest.getChuck();
MultipartFile file = uploadRequest.getMultipartFile();
String chunkPath = fileAddress + "/" + md5File + "/";//分片存放目录 String chunkPath = fileAddress + "/" + md5File + "/";//分片存放目录
File dirFile = new File(chunkPath); File dirFile = new File(chunkPath);
if (!dirFile.exists()) {//目录不存在,创建目录 if (!dirFile.exists()) {//目录不存在,创建目录
...@@ -140,7 +135,7 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -140,7 +135,7 @@ public class FileUploadServiceImpl implements FileUploadService {
chunkName = chunk + ".tmp"; chunkName = chunk + ".tmp";
} }
String filePath = chunkPath + chunkName; String filePath = chunkPath + chunkName;
File saveFile = new File(filePath); File saveFile = new File(new File(filePath).getAbsolutePath());
try { try {
if (!saveFile.exists()) { if (!saveFile.exists()) {
saveFile.createNewFile();//文件不存在,则创建 saveFile.createNewFile();//文件不存在,则创建
...@@ -149,8 +144,6 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -149,8 +144,6 @@ public class FileUploadServiceImpl implements FileUploadService {
} catch (IOException e) { } catch (IOException e) {
log.error(filePath + "文件没有保存"); log.error(filePath + "文件没有保存");
} }
}
}
return ServerResponse.success("true"); return ServerResponse.success("true");
} }
......
...@@ -8,7 +8,7 @@ spring.main.allow-bean-definition-overriding=true ...@@ -8,7 +8,7 @@ spring.main.allow-bean-definition-overriding=true
spring.datasource.url=jdbc:mysql://localhost:3306/ty_efs?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 spring.datasource.url=jdbc:mysql://localhost:3306/ty_efs?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=ljj123456 spring.datasource.password=root
# spring.jpa.properties.hibernate.format_sql=true # spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论