日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

transferto 文件不存在_文件上传时,MultipartFile.transferTo() 方法报 FileNotFoundException...

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 transferto 文件不存在_文件上传时,MultipartFile.transferTo() 方法报 FileNotFoundException... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring Upload?File 報錯FileNotFoundException

環境:

Springboot2.0.4JDK1.8內嵌 Apache Tomcat/8.5.32

1、前端代碼

前端上傳網頁表單,enctype 和 input 的type=file 即可,使用單文件上傳舉例:

圖片

2、后端代碼

@RestController

@RequestMapping("/file")public classUploadFileController {

@Value("${file.upload.path}")private String path = "upload/";

@RequestMapping(value= "fileUpload", method =RequestMethod.POST)

@ResponseBodypublic String fileUpload(@RequestParam("file") MultipartFile file) {if(file.isEmpty()) {return "false";

}

String fileName=file.getOriginalFilename();

File saveFile= new File(path + "/" +fileName);if (!saveFile.getParentFile().exists()) {

saveFile.getParentFile().mkdirs();

}try{

file.transferTo(saveFile);//保存文件

return "true";

}catch(Exception e) {

e.printStackTrace();return "false";

}

}

}

3、問題分析與解決

按照上面配置運行時,在保存文件 file.transferTo(saveFile) 報錯

3.1 問題原因分析:

saveFile是相對路徑,指向 upload/doc20170816162034_001.jpg

file.transferTo 方法調用時,判斷如果是相對路徑,則使用temp目錄,為父目錄

因此,實際保存位置為 C:\Users\xxxx\AppData\Local\Temp\tomcat.372873030384525225.8080\work\Tomcat\localhost\ROOT\upload\doc20170816162034_001.jpg

一則,位置不對,二則沒有父目錄存在,因此產生上述錯誤。

3.2?問題解決

transferTo 傳入參數 定義為絕對路徑

@RestController

@RequestMapping("/file")public classUploadFileController {

@Value("${file.upload.path}")private String path = "upload/";

@RequestMapping(value= "fileUpload", method =RequestMethod.POST)

@ResponseBodypublic String fileUpload(@RequestParam("file") MultipartFile file) {if(file.isEmpty()) {return "false";

}

String fileName=file.getOriginalFilename();

File dest= new File(new File(path).getAbsolutePath()+ "/" +fileName);if (!dest.getParentFile().exists()) {

dest.getParentFile().mkdirs();

}try{

file.transferTo(dest);//保存文件

return "true";

}catch(Exception e) {

e.printStackTrace();return "false";

}

}

}

也可以 file.getBytes() 獲得字節數組,OutputStream.write(byte[] bytes)自己寫到輸出流中。

4、補充方法

application.properties 中增加配置項

spring.servlet.multipart.location= # Intermediate location of uploaded files.

關于上傳文件的訪問

增加一個自定義的ResourceHandler把目錄公布出去

//寫一個Java Config

@Configurationpublic class webMvcConfig implementsorg.springframework.web.servlet.config.annotation.WebMvcConfigurer{//定義在application.properties

@Value("${file.upload.path}")private String path = "upload/";public voidaddResourceHandlers(ResourceHandlerRegistry registry) {

String p= new File(path).getAbsolutePath() + File.separator;//取得在服務器中的絕對路徑

System.out.println("Mapping /upload/** from " +p);

registry.addResourceHandler("/upload/**") //外部訪問地址

.addResourceLocations("file:" + p)//springboot需要增加file協議前綴

.setCacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES));//設置瀏覽器緩存30分鐘

}

}

application.properties 中 file.upload.path=upload/

實際存儲目錄

D:/upload/2019/03081625111.jpg

訪問地址(假設應用發布在http://www.a.com/)

http://www.a.com/upload/2019/03081625111.jpg

在Controller中增加一個RequestMapping,把文件輸出到輸出流中

@RestController

@RequestMapping("/file")public classUploadFileController {

@AutowiredprotectedHttpServletRequest request;

@AutowiredprotectedHttpServletResponse response;

@AutowiredprotectedConversionService conversionService;

@Value("${file.upload.path}")private String path = "upload/";

@RequestMapping(value="/view", method =RequestMethod.GET)public Object view(@RequestParam("id") Integer id){//通常上傳的文件會有一個數據表來存儲,這里返回的id是記錄id

UploadFile file = conversionService.convert(id, UploadFile.class);//這步也可以寫在請求參數中

if(file==null){throw new RuntimeException("沒有文件");

}

File source= new File(new File(path).getAbsolutePath()+ "/" +file.getPath());

response.setContentType(contentType);try{

FileCopyUtils.copy(newFileInputStream(source), response.getOutputStream());

}catch(Exception e) {

e.printStackTrace();

}return null;

}

}

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的transferto 文件不存在_文件上传时,MultipartFile.transferTo() 方法报 FileNotFoundException...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。