whycrzg
2021-04-02 972daa6f2c0d72d40a74387eb0f04acc09750c11
修改 文档材料存储路径
4个文件已修改
108 ■■■■■ 已修改文件
src/main/java/com/whyc/config/StaticResourceConfig.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ProjectManageController.java 55 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/FileUtils.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/config/StaticResourceConfig.java
@@ -1,5 +1,6 @@
package com.whyc.config;
import com.whyc.util.FileUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@@ -24,6 +25,9 @@
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/META-INF/resources/img/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/META-INF/resources/fonts/");
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/uploadFile/**").addResourceLocations("file:///"+ FileUtils.getProjectPath());
        //doc.html静态
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
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);
        }
src/main/java/com/whyc/util/FileUtils.java
@@ -1,8 +1,14 @@
package com.whyc.util;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class FileUtils {
@@ -17,4 +23,46 @@
        out.flush();
        out.close();
    }
    /**
     * 项目同级路径
     * @return
     */
    public static String getFilePath() {
        ApplicationHome ah = new ApplicationHome(FileUtils.class);
        File file = ah.getSource();
//        String projectDir = file.getParentFile().getAbsolutePath();
        String projectDir = file.getAbsolutePath();
        String dir = projectDir + File.separator + "upload"+File.separator;
        System.out.println(dir);
        return dir;
    }
    /**
     * 生成项目同级路径
     * @return
     * @throws FileNotFoundException
     */
    public static String getProjectPath(){
        //获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (!path.exists()) {
            path = new File("");
        }
        //如果上传目录为/static/images/upload/,则可以如下获取
        File upload = new File(path.getAbsolutePath(), "static/upload/");
        if (!upload.exists()) {
            upload.mkdirs();
            //在开发测试模式时,得到地址为:{项目跟目录}/target/static/images/upload/
            //在打成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/
        }
        return upload.toString()+File.separator;
    }
}
src/main/resources/config/application.yml
@@ -65,5 +65,6 @@
  enable: true
#  enable: fase
#com.whyc.config.StaticResourceConfig 下配置对应映射
file:
  filePath: c:/upload/