/** * 保存圖片 */ @CrossOrigin(origins = {"*", "null"}) @RequestMapping(value = "uploadPic", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public String uploadPic(MultipartFile photo) { AppRootBean appRootBean = new AppRootBean(); String resultString = ""; MDC.put("requestType","uploadPic"); logger.info("接收前端data:{}", photo); String s1=""; try { if (null!=photo&&!photo.isEmpty()) { MakeOrderNum makeOrderNum = new MakeOrderNum(); String wx_photo = makeOrderNum.makeOrderNum("wx_photo"); s1 = PicUtil.singleFileUpload(photo,wx_photo); } if (StringUtils.isEmpty(s1)) { resultString = appRootBean.sendRoot(new Object(), ErrorCode.MSG_UPPHONE_CODE_ERROR.getResponseCode(), ErrorCode.MSG_UPPHONE_CODE_ERROR.getResponseMsg()); }else { resultString = appRootBean.sendRoot(s1, ErrorCode.MSG_OK.getResponseCode(), ErrorCode.MSG_OK.getResponseMsg()); } } catch (IOException e) { e.printStackTrace(); } logger.info("上送圖片" + resultString); MDC.clear(); return resultString; }
工具類:
package com.weixiu.jingwei.utils; import net.coobird.thumbnailator.Thumbnails; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Encoder; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.UUID; /** * @Author: 張學濤 * @Date: 2019-08-23 10:25 * @Version 1.0 */ public class PicUtil { private static String UPLOAD_FOLDER = "/opt/wxPhoto/"; public static String singleFileUpload(MultipartFile pc1,String picName) throws IOException { // logger.debug("傳入的文件參數:{}", JSON.toJSONString(file, true)); if (Objects.isNull(pc1) || pc1.isEmpty()) { return ""; } try { byte[] bytes = pc1.getBytes(); Path path = Paths.get(UPLOAD_FOLDER + "/"); if (!Files.isWritable(path)) { Files.createDirectories(path); } String extension = getFileExtension(pc1); //獲取文件后綴 UUID uuid = UUID.randomUUID(); String str = uuid.toString(); String picname = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24); String relativeAddr = picname+picName + extension; //唯一的名字接上后綴 Thumbnails.of(pc1.getInputStream()).size(1920, 1200) //寫入 .outputQuality(1.0f).toFile(path + "/" + relativeAddr); // Thumbnails.of(pc1.getInputStream()) //寫入 // .outputQuality(1.0f).toFile(path + "/" + relativeAddr); //logger.debug("文件寫入成功..."); return Paths.get(path + "/" + relativeAddr).toString(); } catch (IOException e) { // e.printStackTrace(); return "null"; } } //寫一個刪除方法,你們可以看情況修改 public static String deFile(String path) { String resultInfo = null; Map<String, Object> modelMap = new HashMap<String, Object>(); // String sb =area.getAreaName();; // sb.replace("/","\\"); //String sb1 = "F:\\photos\\蛇皮玩意兒\\123\\瞎把.jpg"; File file = new File(path); if (file.exists()) { if (file.delete()) { resultInfo = "1-刪除成功"; } else { resultInfo = "0-刪除失敗"; } } else { resultInfo = "文件不存在!"; } // imgDomainURL: http://10.40.0.53:8888/jeecg-boot/sys/common/view/ return resultInfo; } public static boolean deleteDirectory(String dirPath) {// 刪除目錄(文件夾)以及目錄下的文件 // 如果sPath不以文件分隔符結尾,自動添加文件分隔符 boolean flag = false; String a = null; if (!dirPath.endsWith(File.separator)) { dirPath = dirPath + File.separator; System.out.println("沒有后綴符號'/'"); } File dirFile = new File(dirPath); // 如果dir對應的文件不存在,或者不是一個目錄,則退出 if (!dirFile.exists() || !dirFile.isDirectory()) { System.out.println("不是目錄"); return false; } flag = true; File[] files = dirFile.listFiles();// 獲得傳入路徑下的所有文件 for (int i = 0; i < files.length; i++) {// 循環遍歷刪除文件夾下的所有文件(包括子目錄) if (files[i].isFile()) {// 刪除子文件 a = deFile(files[i].getAbsolutePath()); System.out.println(files[i].getAbsolutePath() + " 目錄文件刪除成功"); if (!flag) { break;// 如果刪除失敗,則跳出 } } else {// 運用遞歸,刪除子目錄 flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) { break;// 如果刪除失敗,則跳出 } } } if (!flag) { return false; } if (dirFile.delete()) { // 刪除當前目錄 return true; } return false; } private static String getFileExtension(MultipartFile File) { String originalFileName = File.getOriginalFilename(); return originalFileName.substring(originalFileName.lastIndexOf(".")); } }