基于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@NoArgsConstructorpublic 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;@Controllerpublic 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)類 */@Servicepublic 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); //控制臺查看服務(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP中if語句、select 、while循環(huán)的使用方法2. ASP中解決“對象關(guān)閉時(shí),不允許操作。”的詭異問題……3. xml中的空格之完全解說4. php bugs代碼審計(jì)基礎(chǔ)詳解5. WMLScript的語法基礎(chǔ)6. ASP使用MySQL數(shù)據(jù)庫的方法7. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. XML入門的常見問題(四)10. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享
