fix:材料校验提交

dev
wangxy 12 months ago
parent e25564aea2
commit 3ce787aa67

@ -7,6 +7,8 @@ import com.hyp.common.exception.ServiceException;
import com.hyp.system.domain.RewFileRelation;
import com.hyp.system.service.RewFileRelationService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
@ -79,6 +81,48 @@ public class FileRelationManager {
return AjaxResult.success();
}
@Transactional(rollbackFor = Exception.class)
public RewFileRelation save(String applyId,String filename,String fileCode,String url,String originalFilename){
RewFileRelation rewFileRelation = new RewFileRelation();
rewFileRelation.setApplyId(applyId);
rewFileRelation.setFileName(filename);
rewFileRelation.setFileCode(fileCode);
rewFileRelation.setFilePath(url);
rewFileRelation.setRealName(originalFilename);
fileRelationService.save(rewFileRelation);
return rewFileRelation;
}
/**
*
*
* @param fileId
* @return java.lang.String
*/
public String getFilePath(String fileId){
RewFileRelation fileRelation = fileRelationService.getById(fileId);
if(Objects.nonNull(fileRelation)){
return fileRelation.getFilePath();
}
return null;
}
/**
*
*
* @param fileId
* @return java.lang.String
*/
public String getRealName(String fileId){
RewFileRelation fileRelation = fileRelationService.getById(fileId);
if(Objects.nonNull(fileRelation)){
return fileRelation.getRealName();
}
return null;
}

@ -1,13 +1,28 @@
package com.hyp.web.controller.reward;
import com.hyp.common.config.HypConfig;
import com.hyp.common.core.controller.BaseController;
import com.hyp.common.core.domain.AjaxResult;
import com.hyp.common.utils.file.FileUploadUtils;
import com.hyp.common.utils.file.FileUtils;
import com.hyp.framework.config.ServerConfig;
import com.hyp.system.domain.RewFileRelation;
import com.hyp.web.controller.manager.FileRelationManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Objects;
/**
@ -22,12 +37,16 @@ import javax.annotation.Resource;
@Api("文件")
@RestController
@RequestMapping("/reward/file")
@Slf4j
public class FileRelationController extends BaseController {
@Resource
private FileRelationManager fileRelationManager;
@Autowired
private ServerConfig serverConfig;
@ApiOperation("材料删除")
@GetMapping("/delFile/{fileId}")
public AjaxResult delFile(@PathVariable String fileId){
@ -51,7 +70,61 @@ public class FileRelationController extends BaseController {
}
@ApiOperation("文件上传")
@PostMapping("/upload")
public AjaxResult uploadFile(MultipartFile file,
@RequestParam String applyId,
@RequestParam String filename,
@RequestParam String fileCode) {
try {
// 上传文件路径
String filePath = HypConfig.getUploadPath();
// 上传并返回新文件名称
String fileNames = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileNames;
String originalFilename = FileUtils.getNameNotSuffix(file.getOriginalFilename()) ;
return AjaxResult.success(fileRelationManager.save(applyId,filename,fileCode,url,originalFilename));
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
@ApiOperation("材料预览")
@GetMapping("fileView/{fileId}")
public void fileView(@PathVariable String fileId, HttpServletResponse response) throws IOException {
String filePath = fileRelationManager.getFilePath(fileId);
String fileName = fileRelationManager.getRealName(fileId);
String encodedFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
InputStream in = null;
OutputStream out = null;
try {
response.setHeader("content-disposition", "attachment;filename=" + encodedFileName + ".pdf");
// 设置文件ContentType类型这样设置会自动判断下载文件类型
response.setContentType("application/octet-stream;charset=utf-8");
File file = new File(filePath);
in = Files.newInputStream(file.toPath());
// 从response对象获取输出流
out = response.getOutputStream();
// 将输入流中的数据读取出来,写到输出流中
for (int b = -1; (b = in.read()) != -1; ) {
out.write(b);
}
} catch (Exception e) {
log.error("获取文件流失败:", e.getMessage(), e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (out != null) {
out.close();
}
}
}
}
}
}

Loading…
Cancel
Save