MultipartFile中transferTo(File file)的路徑問(wèn)題及解決
transferTo(File file)的路徑問(wèn)題
今天看到layui的文件上傳的控件,就嘗試了一下。簡(jiǎn)單創(chuàng)建了一個(gè)SpringMVC項(xiàng)目。記得在配置文件中注入以下Bean。
<!-- 定義文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設(shè)定默認(rèn)編碼 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 設(shè)定文件上傳的最大值為5MB,5*1024*1024 --> <property name="maxUploadSize" value="5242880"></property> <!-- 設(shè)定文件上傳時(shí)寫(xiě)入內(nèi)存的最大值,如果小于這個(gè)參數(shù)不會(huì)生成臨時(shí)文件,默認(rèn)為10240 --> <property name="maxInMemorySize" value="40960"></property> <!-- 上傳文件的臨時(shí)路徑 --> <property name="uploadTempDir" value="fileUpload/temp"></property> <!-- 延遲文件解析 --> <property name="resolveLazily" value="true"/> </bean>
我很懶,這些屬性都沒(méi)有配置,就注冊(cè)了Bean。
接下來(lái)是我出錯(cuò)的地方。先上Controller代碼,前臺(tái)通過(guò)Layui的文件上傳模塊上傳文件。
@ResponseBody @RequestMapping("/upload") public Map upload(HttpServletRequest request,MultipartFile file){ HashMap<String,String> map=new HashMap(); if (!file.isEmpty()) { try { // getOriginalFilename()是包含源文件后綴的全名 String filePath = "D:/upload/test/"+file.getOriginalFilename(); System.out.println(filePath); File saveDir = new File(filePath); if (!saveDir.getParentFile().exists()) saveDir.getParentFile().mkdirs(); file.transferTo(saveDir); map.put("res","上傳成功"); return map; } catch (Exception e) { e.printStackTrace(); } } map.put("res","上傳失敗"); return map; }
transferTo方法中傳遞的file如果是路徑的話,那么它會(huì)將最后一層路徑當(dāng)做文件名,沒(méi)有后綴的那種。此時(shí)重命名這個(gè)文件,更改成和上傳文件一致的后綴那么就可以打開(kāi)了。
比如我將
String filePath = "D:/upload/test/"+file.getOriginalFilename();
改成
String filePath = "D:/upload/test";
運(yùn)行之后打開(kāi)文件發(fā)現(xiàn)這樣的:
transferTo將我想作為文件夾的test當(dāng)做文件名了。我加個(gè)后綴.jpg
和上傳的文件一致。
最后個(gè)人理解為傳入的File參數(shù)是應(yīng)該包含文件而不是文件路徑,transferTo()并不會(huì)將文件轉(zhuǎn)存到文件夾下。
MultipartFile.transferTo( )遇見(jiàn)的問(wèn)題記錄
環(huán)境:
- Springboot 2.0.4
- JDK8
表單,enctype 和 input 的type=file 即可,例子使用單文件上傳
<form enctype="multipart/form-data" method="POST" action="/file/fileUpload"> 圖片<input type="file" name="file" /> <input type="submit" value="上傳" /> </form>
1.文件上傳接值的幾種方式
#spring.servlet.multipart.location=D:/fileupload1
/** * 使用 httpServletRequest作為參數(shù) * @param httpServletRequest * @return */ @PostMapping("/upload") @ResponseBody public Map<String, Object> upload(HttpServletRequest httpServletRequest){ boolean flag = false; MultipartHttpServletRequest multipartHttpServletRequest = null; //強(qiáng)制轉(zhuǎn)換為MultipartHttpServletRequest接口對(duì)象 (它包含所有HttpServletRequest的方法) if(httpServletRequest instanceof MultipartHttpServletRequest){ multipartHttpServletRequest = (MultipartHttpServletRequest) httpServletRequest; }else{ return dealResultMap(false, "上傳失敗"); } //獲取MultipartFile文件信息(注意參數(shù)為前端對(duì)應(yīng)的參數(shù)名稱(chēng)) MultipartFile mf = multipartHttpServletRequest.getFile("file"); //獲取源文件名稱(chēng) String fileName = mf.getOriginalFilename(); //存儲(chǔ)路徑可在配置文件中指定 File pfile = new File("D:/fileupload1/"); if (!pfile.exists()) { pfile.mkdirs(); } File file = new File(pfile, fileName); /* //指定好存儲(chǔ)路徑 File file = new File(fileName);*/ try { //保存文件 //使用此方法保存必須要絕對(duì)路徑且文件夾必須已存在,否則報(bào)錯(cuò) mf.transferTo(file); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); } return dealResultMap(true, "上傳成功"); } /** * 使用Spring MVC的multipartFile 類(lèi)作為參數(shù) * * @param multipartFile * @return */ @PostMapping("/upload/MultipartFile") @ResponseBody public Map<String, Object> uploadMultipartFile(@RequestParam("file") MultipartFile multipartFile){ String fileName = multipartFile.getOriginalFilename(); try { //獲取文件字節(jié)數(shù)組 byte [] bytes = multipartFile.getBytes(); //文件存儲(chǔ)路徑(/fileupload1/ 這樣會(huì)在根目錄下創(chuàng)建問(wèn)價(jià)夾) File pfile = new File("/fileupload1/"); //判斷文件夾是否存在 if(!pfile.exists()){ //不存在時(shí),創(chuàng)建文件夾 pfile.mkdirs(); } //創(chuàng)建文件 File file = new File(pfile, fileName); //寫(xiě)入指定文件夾 OutputStream out = new FileOutputStream(file); out.write(bytes); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); } /*//如果配置文件指定目錄,就可以直接這樣寫(xiě)(不指定路徑的,就需要自己填充保存路徑) File file = new File(fileName); try { //使用此方法保存必須要絕對(duì)路徑且文件夾必須已存在,否則報(bào)錯(cuò) multipartFile.transferTo(file); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); }*/ return dealResultMap(true, "上傳成功"); } @PostMapping("/upload/part") @ResponseBody public Map<String, Object> uploadPart(@RequestParam("file") Part part){ System.out.println(part.getSubmittedFileName()); System.out.println(part.getName()); //輸入流 InputStream inputStream = null; try { inputStream = part.getInputStream(); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); } //保存到臨時(shí)文件 //1K的數(shù)據(jù)緩沖流 byte[] bytes = new byte[1024]; //讀取到的數(shù)據(jù)長(zhǎng)度 int len; //輸出的文件保存到本地文件 File pfile = new File("/fileupload1/"); if (!pfile.exists()) { pfile.mkdirs(); } File file = new File(pfile, part.getSubmittedFileName()); OutputStream out; try { out = new FileOutputStream(file); //開(kāi)始讀取 while ((len = inputStream.read(bytes)) != -1){ out.write(bytes, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); } /*//配置文件配置的有默認(rèn)上傳路徑 //獲取提交文件的名字 String fileName = part.getSubmittedFileName(); try { //使用此方法保存必須要絕對(duì)路徑且文件夾必須已存在,否則報(bào)錯(cuò) part.write(fileName); } catch (IOException e) { e.printStackTrace(); return dealResultMap(false, "上傳失敗"); }*/ return dealResultMap(true, "上傳成功"); }
注意:
MultipartFile.transferTo() 需要的事相對(duì)路徑
file.transferTo 方法調(diào)用時(shí),判斷如果是相對(duì)路徑,則使用temp目錄,為父目錄
一則,位置不對(duì),二則沒(méi)有父目錄存在,因此產(chǎn)生上述錯(cuò)誤。
//1.使用此方法保存必須指定盤(pán)符(在系統(tǒng)配置時(shí)要配絕對(duì)路徑); // 也可以通過(guò) File f = new File(new File(path).getAbsolutePath()+ "/" + fileName); 取得在服務(wù)器中的絕對(duì)路徑 保存即可 // file.transferTo(f); //2.使用此方法保存可相對(duì)路徑(/var/falcon/)也可絕對(duì)路徑(D:/var/falcon/) byte [] bytes = file.getBytes(); OutputStream out = new FileOutputStream(f); out.write(bytes);
2.關(guān)于上傳文件的訪問(wèn)
(1).增加一個(gè)自定義的ResourceHandler把目錄公布出去
// 寫(xiě)一個(gè)Java Config @Configuration public class webMvcConfig implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer{ // 定義在application.properties @Value("${file.upload.path}") private String path = "upload/"; public void addResourceHandlers(ResourceHandlerRegistry registry) { String p = new File(path).getAbsolutePath() + File.separator;//取得在服務(wù)器中的絕對(duì)路徑 System.out.println("Mapping /upload/** from " + p); registry.addResourceHandler("/upload/**") // 外部訪問(wèn)地址 .addResourceLocations("file:" + p)// springboot需要增加file協(xié)議前綴 .setCacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES));// 設(shè)置瀏覽器緩存30分鐘 } }
application.properties 中 file.upload.path=upload/
實(shí)際存儲(chǔ)目錄
D:/upload/2019/03081625111.jpg
(2).在Controller中增加一個(gè)RequestMapping,把文件輸出到輸出流中
@RestController @RequestMapping("/file") public class UploadFileController { @Autowired protected HttpServletRequest request; @Autowired protected HttpServletResponse response; @Autowired protected ConversionService conversionService; @Value("${file.upload.path}") private String path = "upload/"; @RequestMapping(value="/view", method = RequestMethod.GET) public Object view(@RequestParam("id") Integer id){ // 通常上傳的文件會(huì)有一個(gè)數(shù)據(jù)表來(lái)存儲(chǔ),這里返回的id是記錄id UploadFile file = conversionService.convert(id, UploadFile.class);// 這步也可以寫(xiě)在請(qǐng)求參數(shù)中 if(file==null){ throw new RuntimeException("沒(méi)有文件"); } File source= new File(new File(path).getAbsolutePath()+ "/" + file.getPath()); response.setContentType(contentType); try { FileCopyUtils.copy(new FileInputStream(source), response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } return null; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談HttpClient、okhttp和RestTemplate的區(qū)別
這篇文章主要介紹了HttpClient、okhttp和RestTemplate的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java中IO流的BufferedOutputStream和FileOutputStream對(duì)比
這篇文章主要介紹了Java中IO流的BufferedOutputStream和FileOutputStream對(duì)比,不帶緩沖的操作,每讀一個(gè)字節(jié)就要寫(xiě)入一個(gè)字節(jié),由于涉及磁盤(pán)的IO操作相比內(nèi)存的操作要慢很多,所以在讀寫(xiě)的字節(jié)比較少的情況下,效率比較低,需要的朋友可以參考下2023-07-07mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序
這篇文章主要介紹了mybatis resultmap 如何為對(duì)象賦值的調(diào)用順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01MybatisPlus實(shí)現(xiàn)分頁(yè)查詢(xún)和動(dòng)態(tài)SQL查詢(xún)的示例代碼
本文主要介紹了MybatisPlus實(shí)現(xiàn)分頁(yè)查詢(xún)和動(dòng)態(tài)SQL查詢(xún)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Java反射獲取所有Controller和RestController類(lèi)的方法
這篇文章給大家分享了Java反射獲取所有Controller和RestController類(lèi)的方法,文中有詳細(xì)的代碼示例講解,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08Java源碼解析重寫(xiě)鎖的設(shè)計(jì)結(jié)構(gòu)和細(xì)節(jié)
這篇文章主要為大家介紹了Java源碼解析重寫(xiě)鎖的設(shè)計(jì)結(jié)構(gòu)和細(xì)節(jié),這小節(jié)我們以共享鎖作為案列,自定義一個(gè)共享鎖。有需要的朋友可以借鑒參考下2022-03-03springboot整合mongodb并實(shí)現(xiàn)crud步驟詳解
這篇文章主要介紹了springboot整合mongodb并實(shí)現(xiàn)crud,本文分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Java數(shù)組創(chuàng)建的3種方法6種寫(xiě)法代碼示例
這篇文章主要給大家介紹了關(guān)于Java數(shù)組創(chuàng)建的3種方法6種寫(xiě)法,在Java中我們可以使用關(guān)鍵字new來(lái)創(chuàng)建一個(gè)數(shù)組,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01