admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
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
package com.ks.app.controller.admin;
 
import com.ks.app.utils.ImageUtil;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
 
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
 
@Controller
@RequestMapping("/admin/api/file")
public class FileController {
 
 
    @ResponseBody
    @RequestMapping("uploadImg")
    public String uploadImg(@RequestParam("file_url") MultipartFile file, String type, HttpSession session) throws IOException {
        String md5 = DigestUtils.md5Hex(file.getBytes());
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String fileType = contentType.substring(contentType.indexOf("/") + 1);
        String key = "/imgs/" + type + "/" + md5 + "." + fileType;
        FileUploadResult result = COSManager.getInstance().uploadFile(inputStream, key);
        if (result == null) {
            return JsonUtil.loadFalseResult("上传出错");
        }
        JSONObject data = new JSONObject();
        data.put("url", ImageUtil.getCOSImageUrl(key));
        data.put("md5", result.getMd5());
        return JsonUtil.loadTrueResult(data);
    }
 
}