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); } }