Spring Boot實(shí)現(xiàn)圖片上傳功能
本文實(shí)例為大家分享了Spring Boot圖片上傳的具體代碼,供大家參考,具體內(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的配置文件中配置):<br>
* web:
* upload-path: (jar包所在目錄)/resources/static/
*/
@Value("${web.upload-path}")
private String webUploadPath;
/**
* ResultVo是一個(gè)對(duì)象,包含:
* private int errorCode;
* private String errorMsg;
* private Integer total;
* private Object data;
*/
/**
* 基于用戶標(biāo)識(shí)的頭像上傳
* @param file 圖片
* @param userId 用戶標(biāo)識(shí)
* @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, ".");
// 新的圖片文件名 = 獲取時(shí)間戳+"."圖片擴(kuò)展名
String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName;
// 數(shù)據(jù)庫(kù)保存的目錄
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;
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("file", data);
resultVo.setData(resultMap);
resultVo.setError(1, "上傳成功!");
} catch (IOException e) {
resultVo.setError(0, "上傳失敗!");
}
} else {
resultVo.setError(0, "上傳的文件不是圖片類型,請(qǐng)重新上傳!");
}
return resultVo;
} else {
resultVo.setError(0, "上傳失敗,請(qǐng)選擇要上傳的圖片!");
return resultVo;
}
}
}
以上代碼需配置SUserService,一個(gè)業(yè)務(wù)層接口;
一個(gè)ResultVo對(duì)象,屬性已給出;
一個(gè)基于Spring Boot的 .yml配置文件的配置。
訪問圖片的時(shí)候,需要配置.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)容,請(qǐng)點(diǎn)擊 《spring上傳下載專題》進(jìn)行深入學(xué)習(xí)和研究。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring MVC + bootstrap實(shí)現(xiàn)文件上傳示例(帶進(jìn)度條)
- springMVC+ajax實(shí)現(xiàn)文件上傳且?guī)нM(jìn)度條實(shí)例
- springMVC實(shí)現(xiàn)前臺(tái)帶進(jìn)度條文件上傳的示例代碼
- SpringMVC文件上傳 多文件上傳實(shí)例
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- SpringMVC 文件上傳配置,多文件上傳,使用的MultipartFile的實(shí)例
- 詳解SpringMVC使用MultipartFile實(shí)現(xiàn)文件的上傳
- Java Spring MVC 上傳下載文件配置及controller方法詳解
- SpringMVC中MultipartFile上傳獲取圖片的寬度和高度詳解
- 基于SpringBoot實(shí)現(xiàn)圖片上傳與顯示
- SpringMVC上傳圖片與訪問
- springboot帶有進(jìn)度條的上傳功能完整實(shí)例
相關(guān)文章
SpringCloud集成Micrometer Tracing的代碼工程
Micrometer Tracing 是一個(gè)用于微服務(wù)架構(gòu)的追蹤庫(kù),它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)收集和報(bào)告分布式系統(tǒng)中的性能和調(diào)用鏈信息,Micrometer Tracing 旨在幫助開發(fā)者和運(yùn)維人員理解微服務(wù)之間的交互,本文給大家介紹了如何在 Spring Cloud 集成 Micrometer Tracing2024-12-12
java使用compareTo實(shí)現(xiàn)一個(gè)類的對(duì)象之間比較大小操作
這篇文章主要介紹了java使用compareTo實(shí)現(xiàn)一個(gè)類的對(duì)象之間比較大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
基于Springboot的高校社團(tuán)管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)
本文將基于Springboot+Mybatis開發(fā)實(shí)現(xiàn)一個(gè)高校社團(tuán)管理系統(tǒng),系統(tǒng)包含三個(gè)角色:管理員、團(tuán)長(zhǎng)、會(huì)員。文中采用的技術(shù)有Springboot、Mybatis、Jquery、AjAX、JSP等,感興趣的可以了解一下2022-07-07
SpringBoot中使用Quartz管理定時(shí)任務(wù)的方法
這篇文章主要介紹了SpringBoot中使用Quartz管理定時(shí)任務(wù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
log4j2 自動(dòng)刪除過(guò)期日志文件的配置及實(shí)現(xiàn)原理
這篇文章主要介紹了log4j2 自動(dòng)刪除過(guò)期日志文件配置及實(shí)現(xiàn)原理解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
解決mybatis-generator生成器添加類注釋方法無(wú)效的問題
這篇文章主要介紹了解決mybatis-generator生成器添加類注釋方法無(wú)效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享
這篇文章主要介紹了Java實(shí)時(shí)獲取基金收益項(xiàng)目源碼分享,主要包括JAVA爬取天天基金網(wǎng)數(shù)據(jù)使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),需要的朋友可以參考下2021-03-03

