基于springboot實(shí)現(xiàn)文件上傳
本文實(shí)例為大家分享了基于springboot的文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
第一步:在vo包下創(chuàng)建上傳前端響應(yīng)類
import com.alibaba.druid.filter.AutoLoad;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 上傳響應(yīng)參數(shù)
* @param <E>
*/
//以下是lombok插件注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Resp<E> {
//返回狀態(tài)碼 如 200 403
private String code;
//返回信息
private String Msg;
//也可定義為 Object body 都表示任意類型的意思
private E body;//模板類型
/**
* 成功時(shí)候方法
* @param body
* @param <E>
* @return
*/
public static<E> Resp<E> success(E body){
return new Resp<E>("200","上傳成功!",body);
}
/**
* 上傳失敗時(shí)的方法
* @param code
* @param msg
* @param <E>
* @return
*/
public static<E> Resp<E> fail(String code,String msg){
return new Resp<E>(code,msg,null);
}
}
第二步:在controller層接收前端上傳的文件
import com.qf.springboot_ssm_day02.service.UploadService;
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class uploadController {
@Autowired
private UploadService uploadService;
@RequestMapping(value = "upload",method = RequestMethod.POST)
@ResponseBody
//返回類型根據(jù)自定義的返回類型 不一定和我一樣
public Resp<String> upload(@RequestParam("file")MultipartFile file){
return uploadService.upload(file);
}
}
第三步:在servcie包下建立upload接口及其實(shí)現(xiàn)類處理業(yè)務(wù)
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.web.multipart.MultipartFile;
/**
*上傳業(yè)務(wù)類
*/
public interface UploadService {
//上傳接口
Resp<String > upload(MultipartFile file);
}
import com.qf.springboot_ssm_day02.service.UploadService;
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
/**
* 上傳業(yè)務(wù)實(shí)現(xiàn)類
*/
@Service
public class UploadServiceImpl implements UploadService {
@Override
public Resp<String> upload(MultipartFile file) {
//判斷上傳的文件是不是空
if (file.isEmpty()){
return Resp.fail("400","文件為空!");
}
//文件不為空的情況
//獲得原始文件名(前端傳過來的文件名) 帶有拓展名
//原始文件名存在一定問題
String OriginalFilename=file.getOriginalFilename();
//根據(jù) 時(shí)間戳+拓展名=服務(wù)器文件名
// 確定服務(wù)器文件名(經(jīng)過字符操作加上拓展名)
String fileName= System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
//控制臺(tái)查看服務(wù)器文件名
System.out.println(fileName);
//確定文件儲(chǔ)存位置
// 文件保存路徑 注意最后加上雙反斜杠 轉(zhuǎn)義字符所有雙反斜杠
String filePath="F:\\Test\\";
//目標(biāo)文件路徑 (實(shí)際創(chuàng)建在硬盤的文件)
File dest=new File(filePath+fileName);
//判斷dest的父目錄是否存在
if(dest.getParentFile().exists())
dest.getParentFile().mkdirs();
try {
//前端傳過來的文件拷貝在本地
file.transferTo(dest);
}catch (Exception e){
e.printStackTrace();
return Resp.fail("500",OriginalFilename+"上傳失??!");
}
//上傳成功 返回前端穿過來的文件名
return Resp.success(fileName);
}
}
第四步:postman測試上傳


可以看到文件以及成功上傳到本地啦!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot 單文件上傳的實(shí)現(xiàn)步驟
- Spring Boot 2.x 實(shí)現(xiàn)文件上傳功能
- springboot上傳圖片文件步驟詳解
- Spring Boot項(xiàng)目中實(shí)現(xiàn)文件上傳功能的示例
- springboot+thymeleaf 文件上傳功能的實(shí)現(xiàn)代碼
- springboot+vue實(shí)現(xiàn)文件上傳下載
- 文件上傳SpringBoot后端MultipartFile參數(shù)報(bào)空問題的解決辦法
- springboot操作阿里云OSS實(shí)現(xiàn)文件上傳,下載,刪除功能
- SpringBoot實(shí)現(xiàn)上傳文件到AWS S3的代碼
- 解決springboot項(xiàng)目上傳文件出現(xiàn)臨時(shí)文件目錄為空的問題
- SpringBoot實(shí)現(xiàn)本地存儲(chǔ)文件上傳及提供HTTP訪問服務(wù)的方法
- Spring Boot應(yīng)用上傳文件時(shí)報(bào)錯(cuò)的原因及解決方案
相關(guān)文章
springboot中的pom文件?project報(bào)錯(cuò)問題
這篇文章主要介紹了springboot中的pom文件?project報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java如何判斷一個(gè)字符串是否包含某個(gè)字符串
這篇文章主要給大家介紹了關(guān)于Java如何判斷一個(gè)字符串是否包含某個(gè)字符串的相關(guān)資料,在實(shí)際編程中,經(jīng)常需要判斷一個(gè)字符串中是否包含某個(gè)子串,需要的朋友可以參考下2023-07-07
springboot之SpringApplication生命周期和事件機(jī)制解讀
這篇文章主要介紹了springboot之SpringApplication生命周期和事件機(jī)制,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Spring?Boot整合Bootstrap的超詳細(xì)步驟
之前做前端開發(fā),在使用bootstrap的時(shí)候都是去官網(wǎng)下載,然后放到項(xiàng)目中,在頁面引用,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot整合Bootstrap的超詳細(xì)步驟,需要的朋友可以參考下2023-05-05
Java8中的LocalDateTime你會(huì)使用了嗎
LocalDateTime?是?Java?8?中日期時(shí)間?API?提供的一個(gè)類,在日期和時(shí)間的表示上提供了更加豐富和靈活的支持,本文就來講講LocalDateTime的一些具體使用方法吧2023-05-05

