whycrzg
2021-03-30 cb6beb6c390d4dc68617fef6b7ddb356dab1dbf9
update 项目管理/多文件上传
2个文件已添加
5个文件已修改
147 ■■■■■ 已修改文件
src/main/java/com/whyc/config/MyProps.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ProjectManageController.java 93 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/mapper/ProjectManageMapper.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProjectArchiveManageService.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/FileUtils.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application.yml 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/ProjectManageMapper.xml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/config/MyProps.java
New file
@@ -0,0 +1,18 @@
package com.whyc.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="file") //接收application.yml中的file下面的属性
public class MyProps {
    private String filePath;
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
src/main/java/com/whyc/controller/ProjectManageController.java
@@ -1,24 +1,24 @@
package com.whyc.controller;
import com.whyc.config.MyProps;
import com.whyc.dto.Response;
import com.whyc.pojo.ProjectArchiveManage;
import com.whyc.pojo.ProjectManage;
import com.whyc.service.ProjectArchiveManageService;
import com.whyc.service.ProjectProcessManageService;
import com.whyc.util.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
@@ -28,6 +28,9 @@
@RequestMapping("projectManage")
@Api(tags = "项目管理")
public class ProjectManageController {
    @Autowired
    private MyProps myProps;
    @Autowired
    private ProjectProcessManageService service;
@@ -98,7 +101,7 @@
        // 获取后缀名
        // String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径、后期考虑按日期或者月份建dir
        String filePath = "c:/upload/";
        String filePath = myProps.getFilePath();
        // 文件重命名,防止重复,预览时需要显示原文件名 _ 切割
        String fileName = filePath + UUID.randomUUID() + "_" + originalFilename;
        // 文件对象
@@ -127,6 +130,45 @@
        response.setData(map);
        return response;
    }
    @PostMapping("/uploads")
    @ApiOperation(notes = "返回文件地址,确认时提交保存到字段", value = "文档材料添加(多选)")
    public Response uploads(MultipartFile[] files) throws IOException {
        Response<Object> response = new Response<>();
        if (files.length == 0) {
            return response.setMsg(0, "请选择要上传的文件");
        }
        ArrayList<Object> list = new ArrayList<>();
        for (MultipartFile multipartFile : files) {
            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<>();
            try {
                FileUtils.uploadFile(fileBytes, filePath, fileName);
            } catch (IOException e) {
                e.printStackTrace();
                return response.setMsg(0, "上传失败");
            }
            map.put("原文件名:", originalFilename);
            map.put("文件地址:", filePath + fileName);
            list.add(map);
        }
        response.setData(list);
        return response.setMsg(1, "上传成功");
    }
    @GetMapping("/download")
    @ApiOperation(notes = "需要在地址栏测试",value = "文档材料下载")
@@ -310,48 +352,7 @@
    /**
     * 文件上传
     * achievement_name 成果类型
     */
/*
    @RequestMapping("/upload")
    public Response upload(MultipartFile file) {
        try {
            if (file != null) {
                File parentFile = createParentFile();
                String fileName = file.getOriginalFilename();
                String suffix = fileName.substring(fileName.lastIndexOf("."));
                String uuid = IdUtil.simpleUUID();
                fileName = uuid + suffix;
                File imageFile = new File(parentFile, fileName);
                FileUtil.writeFromStream(file.getInputStream(), imageFile);
                */
/**
                 * 年月日目录
                 *//*
                String fileDay = DateUtil.thisYear() + "/" + (DateUtil.thisMonth() + 1) + "/"
                        + DateUtil.thisDayOfMonth();
                String imagePath = SystemConstant.FILE + "/document/" + fileDay + "/" + fileName;
                */
/**
                 * 实时转换
                 *//*
                File toFile = new File(parentFile, uuid + ".pdf");
                documentConverter.convert(imageFile).to(toFile).execute();
                return Result.ok(imagePath);
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            LOGGER.error("转换异常{}",e);
            return Result.error();
        }
    }
*/
}
src/main/java/com/whyc/mapper/ProjectManageMapper.java
@@ -34,6 +34,7 @@
    List<ProjectManage> searchArchiveAll();
//    项目归档管理-条件查询 开始日期 OR 归档日期
    List<ProjectManage> searchArchiveByCondition(ProjectManage projectManage);
    //项目概况信息
src/main/java/com/whyc/service/ProjectArchiveManageService.java
@@ -75,6 +75,11 @@
        return response;
    }
    /**
     * 项目归档管理-条件查询
     * @param projectManage
     * @return
     */
    public Response searchArchiveByCondition(ProjectManage projectManage) {
        Response<Object> response = new Response<>();
src/main/java/com/whyc/util/FileUtils.java
New file
@@ -0,0 +1,20 @@
package com.whyc.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileUtils {
    public static void uploadFile(byte[] file, String filePath, String fileName) throws IOException {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }
}
src/main/resources/config/application.yml
@@ -65,4 +65,5 @@
  enable: true
#  enable: fase
file:
  filePath: c:/upload/
src/main/resources/mapper/ProjectManageMapper.xml
@@ -320,10 +320,11 @@
               pro_execution_date1,
               pro_archive_date
        FROM db_experiment.tb_project_manage
        where pro_num = #{proNum}
        where (pro_num = #{proNum}
            and pro_sort = #{proSort}
            and pro_execution_date = #{proExecutionDate}
           or pro_archive_date = #{proArchiveDate}
            and pro_execution_date = #{proExecutionDate})
           or (pro_num = #{proNum}
            and pro_sort = #{proSort} and pro_archive_date = #{proArchiveDate})
    </select>
    <select id="searchArchiveProjectInfo" resultType="com.whyc.pojo.ProjectManage">