国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

基于springboot實(shí)現(xiàn)文件上傳

瀏覽:3日期:2023-04-10 14:52:32

本文實(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測試上傳

基于springboot實(shí)現(xiàn)文件上傳

基于springboot實(shí)現(xiàn)文件上傳

可以看到文件以及成功上傳到本地啦!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 徐水县| 西青区| 祥云县| 东城区| 惠州市| 古蔺县| 琼中| 青海省| 肇庆市| 漯河市| 蒙山县| 和田县| 武定县| 河津市| 来安县| 仁化县| 茌平县| 鄂伦春自治旗| 庆安县| 分宜县| 玛纳斯县| 迭部县| 天门市| 顺义区| 忻城县| 措勤县| 交城县| 黄浦区| 英山县| 城市| 新疆| 大港区| 南汇区| 吉木萨尔县| 定安县| 台东县| 谷城县| 集安市| 礼泉县| 开鲁县| 阿拉善右旗|