springMVC上传与下载
1.文件上传
(1).在springMVC-config.xml文件配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
(2).控制器方法:
@RequestMapping(value = "/ok")
public void user7(HttpServletRequest req, String name,
@RequestParam(value = "file", required = false) MultipartFile file) {
String filePath = req.getServletContext().getRealPath("/");
String contextPath = req.getContextPath();
String fileName = file.getOriginalFilename();
File destFile = new File(filePath + File.separator + fileName);
try {
file.transferTo(destFile);
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
file.jsp:
<form action="${pageContext.request.contextPath }/ok" enctype="multipart/form-data" method="POST">
<input type="text" name="name" value="12345"><br/>
<input type="file" name="file" value="上传"><br/>
<input type="submit" value="提交">
</form>
2.多文件上传
(1).在springMVC-config.xml文件配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
(2).
file.jsp:
<form action="${pageContext.request.contextPath }/ok" enctype="multipart/form-data" method="POST">
<input type="text" name="name" value="12345"><br/>
<input type="file" name="file" value="上传"><br/>
<input type="file" name="file" value="上传"><br/>
<input type="file" name="file" value="上传"><br/>
<input type="submit" value="提交">
</form>
(3).控制器
@RequestMapping(value = "/ok")
public void user7(MultipartHttpServletRequest req, String name,
@RequestParam(value = "file", required = false) MultipartFile[] file,
Model model) {
String method = req.getMethod();
List<MultipartFile> files = req.getFiles("file");
for (int i = 0; i < files.size(); i++) {
model.addAttribute("file"+i, files.get(i).getOriginalFilename());
}
}
注意:MultipartFile[] 方式试过,只能接收一个,不知道是不是版本问题。
2.文件下载:
@RequestMapping(value = "/down1")
public void user0(HttpServletRequest request, HttpServletResponse response,
Map map) {
try {
File file = new File("D:/你好.txt");
request.setCharacterEncoding("UTF-8");
if(!file.exists()){
response.getWriter().write(new String(file.getName().getBytes("utf-8"),"ISO8859-1"));
return;
}
InputStream in = new FileInputStream(file);
String name = file.getName();
// response.setContentType("text/plain");
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(name.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(file.length()));
ServletOutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = (in.read(b))) != -1) {
out.write(b, 0, b.length);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

浙公网安备 33010602011771号