springboot實現單檔案和多檔案上傳
阿新 • • 發佈:2020-01-07
本文例項為大家分享了springboot實現單檔案/多檔案上傳的具體程式碼,供大家參考,具體內容如下
package com.heeexy.example.controller; import com.alibaba.fastjson.JSONObject; import com.heeexy.example.util.CommonUtil; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.*; @RestController @RequestMapping("/common") public class UploadController { //設定上傳資料夾 File uploadPath = null; //單檔案上傳 @PostMapping("/upload") public JSONObject upload(@RequestParam(value = "file",required = false)MultipartFile file,HttpServletRequest request) throws Exception{ //定義返回客戶端json物件 JSONObject returnData = new JSONObject(); //定義處理流物件 BufferedOutputStream out = null; //將request物件轉成JSONObject物件 JSONObject jsonObject = CommonUtil.request2Json(request); //驗證必填欄位 CommonUtil.hasAllRequired(jsonObject,"user_id,equi_id,upload_type"); //獲取當前使用者id String user_id = jsonObject.getString("user_id"); //獲取當前裝置id String equi_id = jsonObject.getString("equi_id"); //獲取上傳檔案的型別 1:巡檢 2:維保 String upload_type = jsonObject.getString("upload_type"); //判斷上傳檔案型別並設定前置路徑 File uploadPath = null; String basePath = "/root/img"; //基礎檔案上傳路徑 String inspection = "/inspection"; //巡檢資料夾路徑 String maintenance = "/maintenance"; //維保資料夾路徑 switch (upload_type){ case "1": uploadPath = new File(basePath+inspection); break; case "2": uploadPath = new File(basePath+maintenance); break; default: uploadPath = new File(basePath); } //判斷伺服器上傳資料夾是否存在 if(!uploadPath.exists()){ uploadPath.mkdirs(); } //判斷上傳的檔案是否為空 if (file!=null) { //獲取上傳檔案字尾 String houzhui = file.getOriginalFilename().split("\\.")[1]; //拼接上傳檔案儲存路徑(當前使用者id+裝置id+時間戳.字尾名) File fil = new File(uploadPath+"/"+user_id+equi_id+new Date().getTime()+"."+houzhui); try { //將上傳檔案儲存到伺服器上傳資料夾目錄下 out = new BufferedOutputStream(new FileOutputStream(fil)); out.write(file.getBytes()); out.flush(); out.close(); //返回上傳檔案的訪問路徑 getAbsolutePath()返回檔案上傳的絕對路徑 returnData.put("message",fil.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); returnData.put("message","檔案上傳失敗:" + e.getMessage()); } catch (IOException e) { e.printStackTrace(); returnData.put("message","檔案上傳失敗:" + e.getMessage()); }finally { //關閉處理流 if(out!=null){out.close();} } } else { returnData.put("message","檔案上傳失敗,檔案為空"); } return CommonUtil.successJson(returnData); } //多檔案上傳 @PostMapping("/batchUpload") public JSONObject handleFileUpload(HttpServletRequest request) throws Exception{ //定義返回客戶端json物件 JSONObject returnData = new JSONObject(); //定義處理流物件,處理檔案上傳 BufferedOutputStream stream = null; //定義map儲存返回結果集 Map<String,String> returnfileMap = new HashMap<String,String>(); //獲取前端上傳的檔案列表 List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; //將request物件轉成JSONObject物件 JSONObject jsonObject = CommonUtil.request2Json(request); //驗證必填欄位,使用者id,裝置id,上傳檔案型別 CommonUtil.hasAllRequired(jsonObject,upload_type"); //獲取當前使用者id String user_id = jsonObject.getString("user_id"); //獲取當前裝置id String equi_id = jsonObject.getString("equi_id"); //獲取上傳檔案的型別 1:巡檢 2:維保 String upload_type = jsonObject.getString("upload_type"); //判斷上傳檔案型別並設定前置路徑 File uploadPath = null; String basePath = "/root/img"; //基礎檔案上傳路徑 String inspection = "/inspection"; //巡檢資料夾路徑 String maintenance = "/maintenance"; //維保資料夾路徑 switch (upload_type){ case "1": uploadPath = new File(basePath+inspection); break; case "2": uploadPath = new File(basePath+maintenance); break; default: uploadPath = new File(basePath); } //判斷伺服器上傳資料夾是否存在 if(!uploadPath.exists()){ uploadPath.mkdirs(); } //遍歷客戶端上傳檔案列表 for (int i = 0; i < files.size(); ++i) { //獲取到每個檔案 file = files.get(i); try { //獲取上傳檔案字尾 String houzhui = file.getOriginalFilename().split("\\.")[1]; //拼接上傳檔案儲存在伺服器的路徑(當前使用者id+裝置id+時間戳.字尾名) File fil = new File(uploadPath+"/"+user_id+equi_id+new Date().getTime()+"."+houzhui); //將上傳檔案儲存到伺服器上傳資料夾目錄下 byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream(fil)); stream.write(bytes); stream.close(); //每成功上傳一個檔案,將上傳檔名作為key,伺服器儲存路徑作為value存入returnfileMap中 switch (upload_type){ case "1": returnfileMap.put(file.getOriginalFilename(),inspection+"/"+fil.getName()); break; case "2": returnfileMap.put(file.getOriginalFilename(),maintenance+"/"+fil.getName()); break; } } catch (Exception e) { stream = null; //儲存上傳失敗的檔案資訊,value值為"fail",存入returnfileMap中 returnfileMap.put(file.getOriginalFilename(),"fail"); }finally { //關閉處理流 if(stream!=null){stream.close();} } } //返回returnfileMap集合到客戶端 returnData.put("message",returnfileMap); return CommonUtil.successJson(returnData); } }
單檔案檔案結果
多檔案上傳結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。