1 @PostMapping("uploadExcel") 2 public ResponseObj uploadExcel(@RequestParam("excelFile") MultipartFile file,@RequestParam("companyId") String companyId, 3 @RequestParam("productId") String productId,HttpServletRequest request) throws Exception { 4 ResponseObj response = new ResponseObj(); 5 response.setData(Defined.STATUS_SUCCESS); 6 response.setMessage("文件上傳成功!"); 7 ResponseObj resp = new ResponseObj(); 8 resp.setData(Defined.STATUS_ERROR); 9 resp.setMessage("不是文件!"); 10 AliYunFileSetting setting = new AliYunFileSetting(); 11 setting.setAccessKeyId(aliConstants.accessKeyId); 12 setting.setAccessKeySecret(aliConstants.accessKeySecret); 13 setting.setBucketName(aliConstants.bucketName); 14 setting.setEndpoint(aliConstants.endpoint); 15 AliyunFileManager manager = AliyunFileManager.getInstance(setting); 16 if(file.isEmpty()){ 17 response.setData(Defined.STATUS_ERROR); 18 response.setMessage("不是文件!"); 19 return response; 20 } 21 String fileName = file.getOriginalFilename(); 22 long fileSize = file.getSize(); 23 File path = new File(ResourceUtils.getURL("classpath:").getPath()); 24 if(!path.exists()) path = new File(""); 25 File upload = new File(path.getAbsolutePath(),"static/images/upload/"); 26 if(!upload.exists()) upload.mkdirs(); 27 File tempFile = new File(upload+"/"+fileName); 28 if(!tempFile.getParentFile().exists()){ 29 tempFile.getParentFile().mkdirs();//創建父級文件路徑 30 tempFile.createNewFile();//創建文件 31 } 32 String relativePath =aliConstants.excelFilePath;//相對路徑 33 String dir = aliConstants.aliyunHostOuter+"/"+relativePath;//雲端絕對路徑 34 File dest = new File(dir); 35 if(!dest.exists()){ //判斷文件目錄是否存在 36 dest.mkdir();// 37 } 38 try { 39 file.transferTo(tempFile); 40 InputStream is = new FileInputStream(tempFile); 41 String suffix=fileName.substring(fileName.lastIndexOf("."));//獲取原始文件后綴.xlxs(含點) 42 String newFileName = IdGen.uuid()+suffix; 43 boolean result = manager.upload(is, relativePath, newFileName);//上傳文件,並建立隨機文件名。 44 // boolean result = manager.upload(is, dir, fileName);//上傳阿里雲,固定文件名 45 if(result){ 46 response.setData(dir+"/"+newFileName);//返回新建文件名的絕對路徑 47 // 數據庫存入相對路徑 48 BatchRecord batchRecord = new BatchRecord(); 49 batchRecord.setFileName(newFileName); 50 batchRecord.setFilePath(relativePath+"/"+newFileName); 51 batchRecord.setFileSize(fileSize); 52 batchRecord.setIsDelete((byte) 0); 53 batchRecord.setStatus((byte) 0); 54 batchRecord.setId(IdGen.uuid()); 55 batchRecord.setCreateTime(DateUtil.getNowTimestamp()); 56 batchRecord.setUpdateTime(DateUtil.getNowTimestamp()); 57 batchRecord.setCompanyId(companyId); 58 batchRecord.setProductId(productId); 59 Integer resultNum = deviceService.addBatchRecord(batchRecord); 60 if(resultNum>0){ 61 return response; 62 } 63 return resp; 64 }else{ 65 resp.setMessage("文件上傳失敗!"); 66 return resp; 67 } 68 } catch (IllegalStateException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 resp.setMessage("文件上傳異常!"); 72 return resp; 73 } catch (IOException e) { 74 // TODO Auto-generated catch block 75 e.printStackTrace(); 76 resp.setMessage("文件上傳異常!"); 77 return resp; 78 } 79 }
參考的博客https://blog.csdn.net/heylun/article/details/78732451
內容
springboot部署之后無法獲取項目目錄的問題:
之前看到網上有提問在開發一個springboot的項目時,在項目部署的時候遇到一個問題:就是我將項目導出為jar包,然后用java -jar 運行時,項目中文件上傳的功能無法正常運行,其中獲取到存放文件的目錄的絕對路徑的值為空,文件無法上傳。問題鏈接
不清楚此網友具體是怎么實現的,通常我們可以通過如下方案解決:
//獲取跟目錄 File path = new File(ResourceUtils.getURL("classpath:").getPath()); if(!path.exists()) path = new File(""); System.out.println("path:"+path.getAbsolutePath()); //如果上傳目錄為/static/images/upload/,則可以如下獲取: File upload = new File(path.getAbsolutePath(),"static/images/upload/"); if(!upload.exists()) upload.mkdirs(); System.out.println("upload url:"+upload.getAbsolutePath()); //在開發測試模式時,得到的地址為:{項目跟目錄}/target/static/images/upload/ //在打包成jar正式發布時,得到的地址為:{發布jar包目錄}/static/images/upload/
另外使用以上代碼需要注意,因為以jar包發布時,我們存儲的路徑是與jar包同級的static目錄,因此我們需要在jar包目錄的application.properties配置文件中設置靜態資源路徑,如下所示:
#設置靜態資源路徑,多個以逗號分隔 spring.resources.static-locations=classpath:static/,file:static/
以jar包發布springboot項目時,默認會先使用jar包跟目錄下的application.properties來作為項目配置文件。