基于springboot實(shí)現(xiàn)文件上傳
更新時間:2020年11月08日 13:52:53 作者:Tyler Yue
這篇文章主要為大家詳細(xì)介紹了基于springboot實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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;//模板類型 /** * 成功時候方法 * @param body * @param <E> * @return */ public static<E> Resp<E> success(E body){ return new Resp<E>("200","上傳成功!",body); } /** * 上傳失敗時的方法 * @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ù) 時間戳+拓展名=服務(wù)器文件名 // 確定服務(wù)器文件名(經(jīng)過字符操作加上拓展名) String fileName= System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1); //控制臺查看服務(wù)器文件名 System.out.println(fileName); //確定文件儲存位置 // 文件保存路徑 注意最后加上雙反斜杠 轉(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ù)報空問題的解決辦法
- springboot操作阿里云OSS實(shí)現(xiàn)文件上傳,下載,刪除功能
- SpringBoot實(shí)現(xiàn)上傳文件到AWS S3的代碼
- 解決springboot項(xiàng)目上傳文件出現(xiàn)臨時文件目錄為空的問題
- SpringBoot實(shí)現(xiàn)本地存儲文件上傳及提供HTTP訪問服務(wù)的方法
- Spring Boot應(yīng)用上傳文件時報錯的原因及解決方案
相關(guān)文章
springboot之SpringApplication生命周期和事件機(jī)制解讀
這篇文章主要介紹了springboot之SpringApplication生命周期和事件機(jī)制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Spring?Boot整合Bootstrap的超詳細(xì)步驟
之前做前端開發(fā),在使用bootstrap的時候都是去官網(wǎng)下載,然后放到項(xiàng)目中,在頁面引用,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot整合Bootstrap的超詳細(xì)步驟,需要的朋友可以參考下2023-05-05