概述
导入包:
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
单个文件的上传
前台页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Hello World!</h2>
<body>
<form action="${pageContext.request.contextPath }/doupload.action" method="post" enctype="multipart/form-data">
<h2>文件上传</h2>
文件:<input type="file" name="uploadFile"/><br/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</body>
</html>
springmvc.xml中添加:
<!-- 文件上传,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置船上文件的最大尺寸为5MB -->
<property name="maxUploadSize" value="5242880"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>
controller添加:
@RequestMapping("/doupload")
public String doupload(MultipartFile uploadFile,HttpSession session) throws Exception{
String filename = uploadFile.getOriginalFilename();
String leftPath= session.getServletContext().getRealPath("/images");
System.out.println("leftpath======" + leftPath);
File file = new File(leftPath,filename);
uploadFile.transferTo(file);
return "/WEB-INF/jsp/success.jsp";
}
先建立images文件夹。
springmvc文件上传的类是MultipartFile。uploadFile必须与jsp中的name一致。
多个文件的上传
index.jsp:
<form action="${pageContext.request.contextPath }/doupload2.action" method="post" enctype="multipart/form-data">
<h2>文件上传</h2>
文件:<input type="file" name="uploadFile2"/><br/>
<input type="file" name="uploadFile2"/><br/>
<input type="file" name="uploadFile2"/><br/><br/>
<input type="submit" value="上传2"/>
</form>
controller:
@RequestMapping("/doupload2")
public String doupload(@RequestParam MultipartFile[] uploadFile2,HttpSession session) throws Exception{
for(MultipartFile item :uploadFile2){
if(item.getSize()>0){
String filename = item.getOriginalFilename();
String leftPath= session.getServletContext().getRealPath("/images");
File file = new File(leftPath,filename);
item.transferTo(file);
}else{
return "/WEB-INF/jsp/error.jsp";
}
}
return "/WEB-INF/jsp/success.jsp";
}
测试成功。
文件下载
index.jsp
<a href="${pageContext.request.contextPath }/download.action?line.jpg">下载</a>
controller:
@RequestMapping("/download")
public ResponseEntity<byte[]> download() throws IOException {
File file = new File("D:\line.jpg");
HttpHeaders headers = new HttpHeaders();
String filename = new String("hello.jpg".getBytes("utf-8"),"iso-8859-1");
headers.setContentDispositionFormData("attachment",filename);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
测试成功!
最后
以上就是飘逸咖啡豆为你收集整理的java web 之springmvc教程(八)-----实现文件上传及下载的全部内容,希望文章能够帮你解决java web 之springmvc教程(八)-----实现文件上传及下载所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复