springmvc实现简单文件上传和下载
思路:1:maven导入common-fileupload包
2:springmvc.xml导入文件多分布解析器
3:使用MultipartFile获取上传的文件名和转换此文件为服务器上面的指定目录下文件(上传的jsp必须为multipart/form-data,不然multipartfile获取的值为null)
4:下载: 使用FileCopyUtils.copyToByteArray(file)将目标文件转换为二进制数组,设置响应头和下载的文件名称,构建ResponseEntity对象返回到浏览器
1
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"></property>
</bean>
jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data"> 文件 <input type="file" name="aa"/> <a href="${pageContext.request.contextPath}/download?fileName=Servlet.pdf">下载</a> <input type="submit"/> </form> </body> </html>
servlet代码
package com.crazy.goods.tools.fileupload; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; /** * @author Administrator * 创建时间:2017年7月6日上午8:46:53 */ @Controller public class UploadServlet { public String filePath="c:"; @RequestMapping(value="/upload") public String upload(MultipartFile aa,HttpServletResponse response) throws IllegalStateException, IOException { String originalFilename = aa.getOriginalFilename();//获取到的是文件的名字 String name = aa.getName(); //或者的是aa的值 String destFile = filePath+"/"+originalFilename; aa.transferTo(new File(destFile)); //将文件转换为路径下面的文件 response.getWriter().println("upload sucess"); return null; } @RequestMapping(value="/upload1") public String uploadpage() { return "upload"; } @RequestMapping(value="download") public ResponseEntity<byte[]> down(String fileName) throws IOException{ //需要下载的目标文件 File file=new File(filePath+"/"+fileName); //读取目标文件为二进制数组 byte[] fileByte=FileCopyUtils.copyToByteArray(file); //设置响应头 HttpHeaders hh=new HttpHeaders(); //设置下载的文件的名称 hh.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "UTF-8")); //构建ResponseEntity对象 ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(fileByte, hh, HttpStatus.CREATED); return re; } }