最近博主在做SpringMvc文件上傳和下載的功能實現,上網查了很多資料很多都不太符合理想,找啊找,終於找到一個可以用的,然後再此基礎上,我加以改進,可以支持多文件上傳,而且代碼非常精簡,大家可以看看.
http://pan.baidu.com/s/1o7Oo4NC 原碼下載
首先需要導入jar包:
然後,在applicatinContext.xml中添加上傳和下載的配置文件,如下:
<!-- 文件上傳的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上傳文件的總大小不能超過200KB。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="200000"/>
</bean>
<!-- 該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到WebContent目錄下的error.jsp頁面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>
</props>
</property>
</bean>
好了,最基礎的配置就好了,接下來jsp頁面:upload.jsp
<form action="upload.do" method="post" enctype="multipart/form-data">
文件1: <input type="file" name="myfiles"/><br/>
文件2: <input type="file" name="myfiles"/><br/>
文件3: <input type="file" name="myfiles"/><br/>
<input type="submit" value="上傳">
</form>
Controller中對應的java代碼:
@RequestMapping("/upload.do")
public String upload(@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException {
for (MultipartFile file : myfiles) {
// 此處MultipartFile[]表明是多文件,如果是單文件MultipartFile就行了
if (file.isEmpty()) {
System.out.println("文件未上傳!");
} else {
// 得到上傳的文件名
String fileName = file.getOriginalFilename();
// 得到服務器項目發佈運行所在地址
String path1 = request.getSession().getServletContext().getRealPath("image") + File.separator;
// 此處未使用UUID來生成唯一標識,用日期做爲標識
String path = path1 + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + fileName;
// 查看文件上傳路徑,方便查找
System.out.println(path);
// 把文件上傳至path的路徑
File localFile = new File(path);
file.transferTo(localFile);
}
}
return "uploadSuccess";
}
這樣就可以把網頁上選擇的圖片上傳上去了
下載成功了!
文件下載
(文件下載我是參照網上一位前輩的,在此註明他的博客網址:http://my.oschina.net/u/1394615/blog/311307):
download.jsp:此處爲了測試,我直接把文件名當作參數傳過去:
<a href="download.do?fileName=2016082312271111111.jpg">下載</a>
Controller:
@RequestMapping("/download")
public String download(String fileName, HttpServletRequest request, HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
try {
String path = request.getSession().getServletContext().getRealPath("image") + File.separator;
InputStream inputStream = new FileInputStream(new File(path + fileName));
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
// 這裏主要關閉。
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 返回值要注意,要不然就出現下面這句錯誤!
// java+getOutputStream() has already been called for this response
return null;
}
OK,springmvc的上傳下載就完成了!