springMVC上传文件后,在action解析file文件的三种方式。
jsp页面的写法:
<form action="parserUploadFile1" method="post"
enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
action的复制文件写法:
/**
* 1、最原始的输入输出流复制文件
*/
@RequestMapping("/parserUploadFile1")
public String parserUploadFile1(MultipartFile file) throws IOException {
String realPath = "E:/";
InputStream is = file.getInputStream();
String fileName = file.getOriginalFilename();
FileOutputStream os = new FileOutputStream(realPath
+ new Date().getTime() + file.getOriginalFilename());
int i = 0;
while ((i = is.read()) != -1) {
os.write(i);
}
os.flush();
os.close();
is.close();
return "success";
}
/**
* 2、通过apache自带的FileUtils工具类进行复制
*/
@RequestMapping("/parserUploadFile2")
public String parserUploadFile2(MultipartFile file) throws IOException {
String realPath = "E:/";
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename()));
return "success";
}
/**
* 3、通过springMVC提供的API
*/
@RequestMapping("/parserUploadFile3")
public String parserUploadFile3(MultipartFile file) throws IOException {
String realPath = "E:/";
file.transferTo(new File(realPath+file.getOriginalFilename()));
return "success";
}
用任意一种解析file文件即可。项目源码下载地址(免积分):http://download.csdn.net/detail/zl544434558/8555773
原文地址:http://blog.csdn.net/zl544434558/article/details/44816399