基于springboot的檔案上傳
第一步:在vo包下創建上傳前端回應類
import com.alibaba.druid.filter.AutoLoad;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 上傳回應引數
* @param <E>
*/
//以下是lombok插件注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Resp<E> {
//回傳狀態碼 如 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
//回傳型別根據自定義的回傳型別 不一定和我一樣
public Resp<String> upload(@RequestParam("file")MultipartFile file){
return uploadService.upload(file);
}
}
第三步:在servcie包下建立upload介面及其實作類處理業務
import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.web.multipart.MultipartFile;
/**
*上傳業務類
*/
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;
/**
* 上傳業務實作類
*/
@Service
public class UploadServiceImpl implements UploadService {
@Override
public Resp<String> upload(MultipartFile file) {
//判斷上傳的檔案是不是空
if (file.isEmpty()){
return Resp.fail("400","檔案為空!");
}
//檔案不為空的情況
//獲得原始檔案名(前端傳過來的檔案名) 帶有拓展名
//原始檔案名存在一定問題
String OriginalFilename=file.getOriginalFilename();
//根據 時間戳+拓展名=服務器檔案名
// 確定服務器檔案名(經過字符操作加上拓展名)
String fileName= System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
//控制臺查看服務器檔案名
System.out.println(fileName);
//確定檔案儲存位置
// 檔案保存路徑 注意最后加上雙反斜杠 轉義字符所有雙反斜杠
String filePath="F:\\Test\\";
//目標檔案路徑 (實際創建在硬碟的檔案)
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測驗上傳


可以看到檔案以及成功上傳到本地啦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/206173.html
標籤:其他
