MultipartFile中transferTo(File file)的路徑問題及解決
transferTo(File file)的路徑問題
今天看到layui的文件上傳的控件,就嘗試了一下。簡單創(chuàng)建了一個SpringMVC項目。記得在配置文件中注入以下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è)定文件上傳時寫入內(nèi)存的最大值,如果小于這個參數(shù)不會生成臨時文件,默認(rèn)為10240 -->
<property name="maxInMemorySize" value="40960"></property>
<!-- 上傳文件的臨時路徑 -->
<property name="uploadTempDir" value="fileUpload/temp"></property>
<!-- 延遲文件解析 -->
<property name="resolveLazily" value="true"/>
</bean>
我很懶,這些屬性都沒有配置,就注冊了Bean。
接下來是我出錯的地方。先上Controller代碼,前臺通過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如果是路徑的話,那么它會將最后一層路徑當(dāng)做文件名,沒有后綴的那種。此時重命名這個文件,更改成和上傳文件一致的后綴那么就可以打開了。
比如我將
String filePath = "D:/upload/test/"+file.getOriginalFilename();
改成
String filePath = "D:/upload/test";
運行之后打開文件發(fā)現(xiàn)這樣的:

transferTo將我想作為文件夾的test當(dāng)做文件名了。我加個后綴.jpg
和上傳的文件一致。
最后個人理解為傳入的File參數(shù)是應(yīng)該包含文件而不是文件路徑,transferTo()并不會將文件轉(zhuǎn)存到文件夾下。
MultipartFile.transferTo( )遇見的問題記錄
環(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;
//強制轉(zhuǎn)換為MultipartHttpServletRequest接口對象 (它包含所有HttpServletRequest的方法)
if(httpServletRequest instanceof MultipartHttpServletRequest){
multipartHttpServletRequest = (MultipartHttpServletRequest) httpServletRequest;
}else{
return dealResultMap(false, "上傳失敗");
}
//獲取MultipartFile文件信息(注意參數(shù)為前端對應(yīng)的參數(shù)名稱)
MultipartFile mf = multipartHttpServletRequest.getFile("file");
//獲取源文件名稱
String fileName = mf.getOriginalFilename();
//存儲路徑可在配置文件中指定
File pfile = new File("D:/fileupload1/");
if (!pfile.exists()) {
pfile.mkdirs();
}
File file = new File(pfile, fileName);
/* //指定好存儲路徑
File file = new File(fileName);*/
try {
//保存文件
//使用此方法保存必須要絕對路徑且文件夾必須已存在,否則報錯
mf.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
return dealResultMap(false, "上傳失敗");
}
return dealResultMap(true, "上傳成功");
}
/**
* 使用Spring MVC的multipartFile 類作為參數(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();
//文件存儲路徑(/fileupload1/ 這樣會在根目錄下創(chuàng)建問價夾)
File pfile = new File("/fileupload1/");
//判斷文件夾是否存在
if(!pfile.exists()){
//不存在時,創(chuàng)建文件夾
pfile.mkdirs();
}
//創(chuàng)建文件
File file = new File(pfile, fileName);
//寫入指定文件夾
OutputStream out = new FileOutputStream(file);
out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
return dealResultMap(false, "上傳失敗");
}
/*//如果配置文件指定目錄,就可以直接這樣寫(不指定路徑的,就需要自己填充保存路徑)
File file = new File(fileName);
try {
//使用此方法保存必須要絕對路徑且文件夾必須已存在,否則報錯
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, "上傳失敗");
}
//保存到臨時文件
//1K的數(shù)據(jù)緩沖流
byte[] bytes = new byte[1024];
//讀取到的數(shù)據(jù)長度
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);
//開始讀取
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 {
//使用此方法保存必須要絕對路徑且文件夾必須已存在,否則報錯
part.write(fileName);
} catch (IOException e) {
e.printStackTrace();
return dealResultMap(false, "上傳失敗");
}*/
return dealResultMap(true, "上傳成功");
}
注意:
MultipartFile.transferTo() 需要的事相對路徑
file.transferTo 方法調(diào)用時,判斷如果是相對路徑,則使用temp目錄,為父目錄
一則,位置不對,二則沒有父目錄存在,因此產(chǎn)生上述錯誤。
//1.使用此方法保存必須指定盤符(在系統(tǒng)配置時要配絕對路徑);
// 也可以通過 File f = new File(new File(path).getAbsolutePath()+ "/" + fileName); 取得在服務(wù)器中的絕對路徑 保存即可
// file.transferTo(f);
//2.使用此方法保存可相對路徑(/var/falcon/)也可絕對路徑(D:/var/falcon/)
byte [] bytes = file.getBytes();
OutputStream out = new FileOutputStream(f);
out.write(bytes);
2.關(guān)于上傳文件的訪問
(1).增加一個自定義的ResourceHandler把目錄公布出去
// 寫一個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ù)器中的絕對路徑
System.out.println("Mapping /upload/** from " + p);
registry.addResourceHandler("/upload/**") // 外部訪問地址
.addResourceLocations("file:" + p)// springboot需要增加file協(xié)議前綴
.setCacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES));// 設(shè)置瀏覽器緩存30分鐘
}
}
application.properties 中 file.upload.path=upload/
實際存儲目錄
D:/upload/2019/03081625111.jpg
(2).在Controller中增加一個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){
// 通常上傳的文件會有一個數(shù)據(jù)表來存儲,這里返回的id是記錄id
UploadFile file = conversionService.convert(id, UploadFile.class);// 這步也可以寫在請求參數(shù)中
if(file==null){
throw new RuntimeException("沒有文件");
}
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;
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中如何將符號分隔的文本文件txt轉(zhuǎn)換為excel
這篇文章主要介紹了Java中如何將符號分隔的文本文件txt轉(zhuǎn)換為excel,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09

