Browse Source

文件下载2

zhuhaiwen 3 years ago
parent
commit
d8757ed20f

+ 6 - 0
oa-app/src/main/java/com/css/oa/exam/announce/controller/AnnounceController.java

66
    @GetMapping("/download")
66
    @GetMapping("/download")
67
    public void download(@RequestParam(value = "uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
67
    public void download(@RequestParam(value = "uuid") String uuid, HttpServletRequest request, HttpServletResponse response) {
68
        try {
68
        try {
69
            if(TextUtils.isEmpty(uuid)){
70
                exportPrint(response, "未找到此文件!");
71
                return;
72
            }
73
            String token = getToken();
74
            mService.setToken(token);
69
            mService.download(uuid, request, response);
75
            mService.download(uuid, request, response);
70
        } catch (Exception e1) {
76
        } catch (Exception e1) {
71
            e1.printStackTrace();
77
            e1.printStackTrace();

+ 2 - 2
oa-app/src/main/java/com/css/oa/exam/announce/repository/Announce.java

44
    @Column(name = "IS_TOP")
44
    @Column(name = "IS_TOP")
45
    public int is_top;
45
    public int is_top;
46
46
47
    @Column(name = "FILE_PATH")
48
    public String file_path;
47
    @Column(name = "FILE_ID")
48
    public String file_id;
49
49
50
    @Transient
50
    @Transient
51
    public MultipartFile file;
51
    public MultipartFile file;

+ 12 - 8
oa-app/src/main/java/com/css/oa/exam/announce/service/AnnounceService.java

39
    @Override
39
    @Override
40
    public void add(Announce obj) {
40
    public void add(Announce obj) {
41
        System.out.println("传入的obj => " + obj.toString());
41
        System.out.println("传入的obj => " + obj.toString());
42
        String file_id = "";
42
        //上传文件
43
        //上传文件
43
        if (obj.file != null) {
44
        if (obj.file != null) {
44
            Admin admin = Admin.getAdminByToken(token);
45
            Admin admin = Admin.getAdminByToken(token);
45
            new UploadService().upload(obj.file, admin);
46
            file_id = new UploadService().upload(obj.file, admin);
46
        }
47
        }
48
        obj.setFile_id(file_id);
47
        String id = UUIDGenerator.getUUID();
49
        String id = UUIDGenerator.getUUID();
48
        obj.setUuid(id);
50
        obj.setUuid(id);
49
        Date newDate = new Date();
51
        Date newDate = new Date();
66
    }
68
    }
67
69
68
    /*
70
    /*
69
    *  @param uuid = OP_XM_ENCLOSURE表主键
70
    */
71
     *  @param uuid = OP_XM_ENCLOSURE表主键
72
     */
71
    @Override
73
    @Override
72
    public void download(String uuid, HttpServletRequest request, HttpServletResponse response) {
74
    public void download(String uuid, HttpServletRequest request, HttpServletResponse response) {
73
        Admin admin = Admin.getAdminByToken(token);
75
        Admin admin = Admin.getAdminByToken(token);
77
    @Override
79
    @Override
78
    public void update(Announce newObj) {
80
    public void update(Announce newObj) {
79
        //上传文件
81
        //上传文件
82
        String file_id = "";
80
        if (newObj.file != null) {
83
        if (newObj.file != null) {
81
            Admin admin = Admin.getAdminByToken(token);
84
            Admin admin = Admin.getAdminByToken(token);
82
            new UploadService().upload(newObj.file, admin);
85
            file_id = new UploadService().upload(newObj.file, admin);
83
        }
86
        }
84
        Optional<Announce> byId = repository.findById(newObj.getUuid());
85
        Announce old = byId.get();
86
        CopyObjTool.copyProperties(old, newObj);
87
        repository.save(newObj);
87
        Announce old = repository.findById(newObj.getUuid()).get();
88
        if(!TextUtils.isEmpty(file_id)){
89
            old.setFile_id(file_id);
90
        }
91
        repository.save(old);
88
    }
92
    }
89
93
90
    @Override
94
    @Override

+ 3 - 2
oa-app/src/main/java/com/css/oa/exam/util/upload/UploadService.java

23
@Slf4j
23
@Slf4j
24
public class UploadService {
24
public class UploadService {
25
25
26
    public void upload(MultipartFile file, Admin admin) {
26
    public String upload(MultipartFile file, Admin admin) {
27
        //1,先上传
27
        //1,先上传
28
        JSONObject json = UploadUtil.upload(file, admin);
28
        JSONObject json = UploadUtil.upload(file, admin);
29
        //2,保存数据
29
        //2,保存数据
31
31
32
        if (json.containsKey("result") && "false".equals(json.getString("result"))) {
32
        if (json.containsKey("result") && "false".equals(json.getString("result"))) {
33
            log.debug("上传文档报错:" + json.getString("message"));
33
            log.debug("上传文档报错:" + json.getString("message"));
34
            return;
34
            return "";
35
        }
35
        }
36
        FileInfoEntity entity = entity = new FileInfoEntity();
36
        FileInfoEntity entity = entity = new FileInfoEntity();
37
        entity.setUuid(UUIDGenerator.getUUID());
37
        entity.setUuid(UUIDGenerator.getUUID());
57
        entity.setDelFlag(0);
57
        entity.setDelFlag(0);
58
        entity.setIsValid(1);
58
        entity.setIsValid(1);
59
        fileInfoRepository.save(entity);
59
        fileInfoRepository.save(entity);
60
        return entity.getUuid();
60
    }
61
    }
61
62
62
    @Autowired
63
    @Autowired