whyczh
2021-04-07 9f83bdaebfd57892bc48857c286780a01efeb581
src/main/java/com/whyc/controller/ProjectManageController.java
@@ -2,6 +2,7 @@
import com.github.pagehelper.PageInfo;
import com.whyc.config.MyProps;
import com.whyc.config.ServerConfig;
import com.whyc.dto.Response;
import com.whyc.pojo.ProjectArchiveManage;
import com.whyc.pojo.ProjectManage;
@@ -34,6 +35,9 @@
    @Autowired
    private MyProps myProps;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private ProjectProcessManageService service;
@@ -75,6 +79,7 @@
        return response;
    }
    /**
     * 文件存储后地址更新到对应项目编号的地址,一个项目对应多个文件 db_experiment.tb_project_archive_manage
     * 是否需要将word转pdf/预览文件pdf
@@ -82,45 +87,6 @@
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ApiOperation(notes = "返回文件地址,确认时提交保存到字段", value = "项目过程管理-文档材料添加(选择文件)")
    public Response upload(@RequestParam("file") MultipartFile file) {
        // 获取原始名字
        String originalFilename = file.getOriginalFilename();
        // 获取后缀名
        // String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径、后期考虑按日期或者月份建dir
        String filePath = myProps.getFilePath();
        // 文件重命名,防止重复,预览时需要显示原文件名 _ 切割
        String fileName = filePath + UUID.randomUUID() + "_" + originalFilename;
        // 文件对象
        File dest = new File(fileName);
        // 判断路径是否存在,如果不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        Response<Object> response = new Response<>();
        HashMap<Object, Object> map = new HashMap<>();
        try {
            // 保存到服务器中
            file.transferTo(dest);
            map.put("fileType", file.getContentType());
            map.put("name", originalFilename);
            Double size = file.getSize() * 0.01 / 1024 / 1024 * 100;
            String douStr = String.format("%.2f", size);
            map.put("fileSize", douStr+" M");
            map.put("url",fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return response.setMsg(0, "上传失败");
        }
        response.setCode(1);
        response.setData(map);
        return response;
    }
    @PostMapping("/uploads")
    @ApiOperation(notes = "返回文件地址,确认时提交保存到字段", value = "项目过程管理-文档材料添加(多选)")
    public Response uploads(MultipartFile[] file) throws IOException {
@@ -130,26 +96,29 @@
        }
        ArrayList<Object> list = new ArrayList<>();
//            String filePath = myProps.getFilePath();
        String uploadPath = FileUtils.getProjectPath();
//        System.out.println("uploadPath = " + uploadPath);
        for (MultipartFile multipartFile : file) {
            if (multipartFile.isEmpty()) {
                return response.setMsg(0, "请选择要上传的文件");
            }
            byte[] fileBytes = multipartFile.getBytes();
            String filePath = myProps.getFilePath();
            //取得当前上传文件的文件名称
            String originalFilename = multipartFile.getOriginalFilename();
            //生成文件名
            String fileName = UUID.randomUUID() + "_" + originalFilename;
            HashMap<Object, Object> map = new HashMap<>();
            map.put("name", originalFilename);
            String url = serverConfig.getUrl();
            map.put("url", url + "/uploadFile/" + fileName);
            try {
                FileUtils.uploadFile(fileBytes, filePath, fileName);
                FileUtils.uploadFile(fileBytes, uploadPath, fileName);
            } catch (IOException e) {
                e.printStackTrace();
                return response.setMsg(0, "上传失败");
            }
            map.put("fileName", originalFilename);
            map.put("url", filePath + fileName);
            list.add(map);
        }
@@ -159,10 +128,18 @@
    @GetMapping("/download")
    @ApiOperation(notes = "需要在地址栏测试",value = "项目过程管理-文档材料下载")
    public Object download(HttpServletResponse response, @RequestParam String filePath,@RequestParam String fileName) {
        Response<Object> result = new Response<>();
        File file = new File( filePath);
    @ApiOperation(notes = "需要在地址栏测试 http://localhost:8090/projectManage/download?fileName=申请书.pdf",value = "项目过程管理-文档材料下载")
    public String download(HttpServletResponse response, @ApiParam(value = "文件路径",required = true)@RequestParam String filePath) {
        String fileName = "";
        if (filePath.length() > 10&&filePath.contains("/")) {   //IP地址+文件名
            int lastIndexOf = filePath.lastIndexOf("/");
            fileName = filePath.substring(lastIndexOf);
        } else {
            return "filePath is Error";
        }
        String projectPath = FileUtils.getProjectPath();
        File file = new File( projectPath+fileName);
        if (file.exists()) {
            try {
                fileName= new String(fileName.getBytes("gbk"), "ISO8859-1");
@@ -184,10 +161,12 @@
                    outputStream.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                return result.setMsg(1,"下载成功");
//               return result.setMsg(1,"下载成功");
               return "downloadSuccessful";
            } catch (Exception e) {
                e.printStackTrace();
                return result.setMsg(0,"下载失败");
//                return result.setMsg(0,"下载失败");
                return "downloadFailed";
            } finally {
                if (bis != null) {
                    try {
@@ -204,8 +183,9 @@
                    }
                }
            }
        }else{
            return "fileDoesNotExist";
        }
        return result.setMsg(0,"文件不存在");
    }
@@ -307,17 +287,13 @@
    /**
     * @param proNameCode
     * @param proNum
     * @return  实验名称、上传日期、归档日期 (操作TODO)
     * @return 实验名称、试验日期、结论字段 etc
     */
    @GetMapping("archiveExperimentData")
    @ApiOperation(notes = "(操作TODO)",value = "项目归档管理-试验数据")
    public Response searchArchiveExperimentData(@ApiParam(value = "项目名称代码", required = true) @RequestParam String proNameCode,
                                                                 @ApiParam(value = "项目编号", required = true) @RequestParam String proNum) {
        ProjectArchiveManage pam = new ProjectArchiveManage();
        pam.setProNum(proNum);
        pam.setProNameCode(proNameCode);
        return archiveManageService.searchArchiveExperimentData(pam);
    @ApiOperation(notes = "TODO 文件格式和下载地址", value = "项目归档管理-试验数据")
    public Response searchArchiveExperimentData(@ApiParam(value = "项目名称代码", required = true) @RequestParam String proNameCode) {
    //TODO fileName file
        return archiveManageService.searchArchiveExperimentData(proNameCode);
    }
    @GetMapping("archiveTechnologicalAchievements")