whycrzg
2021-03-30 188601ec04cee3beadabfd2a2e957b76c7ce5e62
update 项目管理/文件上传
1个文件已修改
76 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/ProjectManageController.java 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ProjectManageController.java
@@ -13,8 +13,10 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
@@ -84,38 +86,98 @@
    /**
     * 文件存储后地址更新到对应项目编号的地址,一个项目对应多个文件 db_experiment.tb_project_archive_manage
     * 是否需要将word转pdf/预览文件pdf
     *
     * @param file
     * @return
     */
    @PostMapping("upload")
    @ApiOperation(notes = "返回文件地址,确认时提交保存到字段",value = "文档材料添加(选择文件)")
    @ApiOperation(notes = "返回文件地址,确认时提交保存到字段", value = "文档材料添加(选择文件)")
    public Response upload(@RequestParam("file") MultipartFile file) {
        // 获取原始名字
        String fileName = file.getOriginalFilename();
        String originalFilename = file.getOriginalFilename();
        // 获取后缀名
        // String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径、后期考虑按日期或者月份建dir
        String filePath = "c:/upload/";
        // 文件重命名,防止重复,预览时需要显示原文件名 _ 切割
        fileName = filePath + UUID.randomUUID() + "_" + fileName;
        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("文件类型:", file.getContentType());
            map.put("原文件名:", originalFilename);
            map.put("是否为空:", file.isEmpty());
            Double size = file.getSize() * 0.01 / 1024 / 1024 * 100;
            String douStr = String.format("%.2f", size);
            map.put("文件大小:", douStr+" M");
            map.put("文件地址:",fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response().setCode(0);
            return response.setMsg(0, "上传失败");
        }
        Response<Object> response = new Response<>();
        response.setMsg(1, fileName);
        response.setCode(1);
        response.setData(map);
        return response;
    }
    @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);
        if (file.exists()) {
            try {
                fileName= new String(fileName.getBytes("gbk"), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/force-download");
            // 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream outputStream = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    outputStream.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                return result.setMsg(1,"下载成功");
            } catch (Exception e) {
                e.printStackTrace();
                return result.setMsg(0,"下载失败");
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return result.setMsg(0,"文件不存在");
    }
    @GetMapping("managesState")
    @ApiOperation(notes = "", value = "项目管理-己确认/未确认阶段查询")