forked from kidgrow-microservices-platform

bingbing
2020-10-27 1d5345f92dbd4aac2b5f68265921bb2529a71b8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.kidgrow.filecenter.controller;
 
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.Result;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.filecenter.model.FileInfo;
import com.kidgrow.filecenter.service.IFileService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 文件上传<br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/18 11:41 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@RestController
public class FileController {
    @Resource
    private IFileService fileService;
 
    /**
     * 文件上传
     * 根据fileType选择上传方式
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value="/files-anon",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo upload(@RequestParam("file") MultipartFile file) throws Exception {
        return fileService.upload(file);
    }
 
 
    /**
     * 文件上传
     * 根据fileType选择上传方式
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value="/files-upload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo feignUpload(@RequestPart("file") MultipartFile file,@RequestParam String imgType) throws Exception {
        return fileService.upload(file,imgType);
    }
    /**
     * 文件上传 返回带缩略图地址的对象,缩略图在path字段
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping(value="/files-thupload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo thUpload(@RequestPart("file") MultipartFile file,@RequestParam String imgType) throws Exception {
        return fileService.uploadForThumbnails(file,imgType);
    }
    /**
     * 文件上传 根据fileType选择上传方式 hospitalId  departmentId
     * @param file
     * @return
     */
    @PostMapping(value="/feignUploadHosIdAndDepId",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo feignUploadHosIdAndDepId(@RequestPart("file") MultipartFile file,@RequestParam("imgType") String imgType,@RequestParam("hospitalId") String hospitalId,@RequestParam("departmentId") String departmentId) throws Exception {
        return fileService.feignUploadHosIdAndDepId(file,imgType,hospitalId,departmentId);
    }
    /**
     * base64上传图片
     */
    @PostMapping(value="baseUplaod",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo baseUplaod(@RequestParam("file") String file,@RequestParam("imgType") String imgType,
                               @RequestParam("hospitalId") String hospitalId,@RequestParam("departmentId") String departmentId){
        return fileService.baseUplaod(file,imgType,hospitalId,departmentId);
    }
    /**
     * base64图片下载
     */
    @PostMapping(value="baseDownLoad")
    public ResultBody baseDownLoad(@RequestBody Map<String,Object> map) throws Exception{
        String url = MapUtils.getString(map, "url");
        if (StringUtils.isBlank(url)) {
            return ResultBody.failed("请输入地址");
        }
        return fileService.baseDownLoad(map);
    }
    /**
     * byte[]上传
     */
    @PostMapping(value="byteUplaod")
    public FileInfo byteUplaod(@RequestParam("fileName") String fileName,@RequestBody byte[] bytes) throws IOException{
        if(bytes.length == 0){
            return null;
        }
        if (com.kidgrow.common.utils.StringUtils.isBlank(fileName)) {
            return null;
        }
        InputStream inputStream = new ByteArrayInputStream(bytes);
        return fileService.byteUplaod(fileName,inputStream,bytes.length);
    }
    /**
     * byte[]上传
     */
    @PostMapping(value="byteUplaodTest",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FileInfo byteUplaodTest(@RequestParam("fileName") String fileName,@RequestPart("file") MultipartFile file)throws IOException {
        InputStream inputStream = file.getInputStream();
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int ch;
        /**
         *
         * */
        while ((ch = inputStream.read(buffer)) != -1) {
            bytestream.write(buffer,0,ch);
        }
        byte[] data = bytestream.toByteArray();
        InputStream inputStream1 = new ByteArrayInputStream(data);
        return fileService.byteUplaod(fileName,inputStream,data.length);
    }
 
    /**
     * 文件删除
     *
     * @param id
     */
    @DeleteMapping("/files/{id}")
    public Result delete(@PathVariable String id) {
        try {
            fileService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            return Result.failed("操作失败");
        }
    }
 
    /**
     * 文件查询
     *
     * @param params
     * @return
     */
    @GetMapping("/files")
    public PageResult<FileInfo> findFiles(@RequestParam Map<String, Object> params) {
        return fileService.findList(params);
    }
}