十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
利用Spring Boot怎么樣實現(xiàn)一個圖片上傳功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
交口網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),交口網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為交口1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的交口做網(wǎng)站的公司定做!
具體內(nèi)容如下
package com.clou.inteface.domain.web.user; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * 文件上傳 * @author Fly * */ @RestController public class FileUpload { /** * 用戶管理 -> 業(yè)務(wù)層 */ @Autowired private SUserService sUserService; /** * 文件上傳根目錄(在Spring的application.yml的配置文件中配置):
* web: * upload-path: (jar包所在目錄)/resources/static/ */ @Value("${web.upload-path}") private String webUploadPath; /** * ResultVo是一個對象,包含: * private int errorCode; * private String errorMsg; * private Integer total; * private Object data; */ /** * 基于用戶標(biāo)識的頭像上傳 * @param file 圖片 * @param userId 用戶標(biāo)識 * @return */ @PostMapping(value = "/fileUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResultVo fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("userId") Integer userId) { ResultVo resultVo = new ResultVo(); if (!file.isEmpty()) { if (file.getContentType().contains("image")) { try { String temp = "images" + File.separator + "upload" + File.separator; // 獲取圖片的文件名 String fileName = file.getOriginalFilename(); // 獲取圖片的擴(kuò)展名 String extensionName = StringUtils.substringAfter(fileName, "."); // 新的圖片文件名 = 獲取時間戳+"."圖片擴(kuò)展名 String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName; // 數(shù)據(jù)庫保存的目錄 String datdDirectory = temp.concat(String.valueOf(userId)).concat(File.separator); // 文件路徑 String filePath = webUploadPath.concat(datdDirectory); File dest = new File(filePath, newFileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } // 判斷是否有舊頭像,如果有就先刪除舊頭像,再上傳 SUser userInfo = sUserService.findUserInfo(userId.toString()); if (StringUtils.isNotBlank(userInfo.getUserHead())) { String oldFilePath = webUploadPath.concat(userInfo.getUserHead()); File oldFile = new File(oldFilePath); if (oldFile.exists()) { oldFile.delete(); } } // 上傳到指定目錄 file.transferTo(dest); // 將圖片流轉(zhuǎn)換進(jìn)行BASE64加碼 //BASE64Encoder encoder = new BASE64Encoder(); //String data = encoder.encode(file.getBytes()); // 將反斜杠轉(zhuǎn)換為正斜杠 String data = datdDirectory.replaceAll("\\\\", "/") + newFileName; MapresultMap = new HashMap<>(); resultMap.put("file", data); resultVo.setData(resultMap); resultVo.setError(1, "上傳成功!"); } catch (IOException e) { resultVo.setError(0, "上傳失敗!"); } } else { resultVo.setError(0, "上傳的文件不是圖片類型,請重新上傳!"); } return resultVo; } else { resultVo.setError(0, "上傳失敗,請選擇要上傳的圖片!"); return resultVo; } } }
以上代碼需配置SUserService,一個業(yè)務(wù)層接口;
一個ResultVo對象,屬性已給出;
一個基于Spring Boot的 .yml配置文件的配置。
訪問圖片的時候,需要配置.yml文件
spring:
#配置http訪問服務(wù)器圖片的路徑
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
然后基于服務(wù)的IP與端口,http//IP:port/resources/static/圖片路徑(圖片名)
看完上述內(nèi)容,你們掌握利用Spring Boot怎么樣實現(xiàn)一個圖片上傳功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!