whycrzg
2021-04-02 972daa6f2c0d72d40a74387eb0f04acc09750c11
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);
        }