springmvc檔案上傳/下載
阿新 • • 發佈:2018-12-21
檔案上傳
1,配置檔案上傳解析器
在springmvc-servlet.xml中配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設定上傳檔案的最大尺寸為1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </bean>
需要匯入fileupload依賴包 io的包
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.io-1.4.0.jar
2,編寫前端頁面程式碼,使用form表單上傳
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/11/7 Time: 15:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>檔案上傳</title> </head> <body> <form action="uploadFile" method="post" enctype="multipart/form-data"> <input type="file" name="file"><br> <input type="submit" value="提交"> </form> </body> </html>
3,controller裡面的程式碼,
一定使用MultipartFile file作為形參,能將前端傳入的檔案自動注入到該引數中。
@RequestMapping("/uploadFile") public String uploadFile(MultipartFile file){ System.out.println(file.getContentType());//獲取檔案型別 System.out.println(file.getSize());//獲取檔案大小 System.out.println(file.getOriginalFilename());//獲取檔案原始名稱 System.out.println(file.getName());//input的name //把檔案儲存在指定路徑(桌面) try { File desFile = new File("C:/Users/Administrator/Desktop/"+file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(),desFile); } catch (IOException e) { e.printStackTrace(); } return "index"; }
4,使用ajax上傳圖片,動態顯示在傍邊的img標籤中
ajax的特點:非同步請求,區域性重新整理
前端加上jquary程式碼
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/11/9
Time: 19:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript">
$(function(){
$("#file").change(function(){
var data = new FormData($("#form")[0]);
$.ajax({
type:"post",
url:"uploadFile1",
data:data,
cache:false,
processData:false,
contentType:false,
beforeSend:function(){
},
success:function(msg){
$("img").attr("src",msg);
},
error:function(){
}
});
});
});
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" id="form">
<input type="file" name="file" id="file"><img style="width: 100px;height: 100px"><br>
</form>
</body>
</html>
controller程式碼
@RequestMapping("/uploadFile1")
@ResponseBody//區域性重新整理
public String uploadFile1(MultipartFile file ,HttpServletRequest request){
//把檔案儲存在指定路徑
String path = request.getServletContext().getRealPath("imgs");
File destFile = new File(path+"/"+file.getOriginalFilename());
try {
InputStream in = file.getInputStream();
FileUtils.copyInputStreamToFile(in,destFile);
} catch (IOException e) {
e.printStackTrace();
}
String msg = "imgs/"+file.getOriginalFilename();
return msg;
}
5,檔案下載
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletResponse response) throws Exception{
response.setHeader("content-Disposition","attachment;filename = a.jpg");
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream("D:\\file\\1.jpg");
IOUtils.copy(in,out);
in.close();
out.close();
}
直接訪問/download就能下載檔案,不過現在都不用這種方法下載了,大都使用html5的新特性下載檔案。