用SpringMVC实现文件的上传下载,首先创建一个web工程,添加jar包
1.文件上传
spring-servlet.xml配置文件
<!-- 开启扫描 -->
<context:component-scan base-package="com.sxt.controller"></context:component-scan>
<!-- 开启springmvc注解的方式 -->
<mvc:annotation-driven></mvc:annotation-driven>
web.xml文件
**<!-- 配置springMVC的前端控制器 中央控制器 -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>关联SpringMVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 配置SpringMVC的映射地址 -->
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>**
user.jsp页面,注意表单的提交方式一定要用post方式
那么在controller里面图片要怎么去接收呢,在中央控制器和controller中中央控制器会对文件做解析操作,如果上传的是文件的话,所以无需自己去解析,注意controller的形参文件名必须和要提交的文件名保持一致,transferTo是一个文件复制方法
@Controller
public class UserController {
@RequestMapping("/upload")
@ResponseBody
public void upload(String name,MultipartFile file1) throws IllegalStateException, IOException {
System.out.println(name+" "+file1.getOriginalFilename());
file1.transferTo(new File("c:/test/"+file1.getOriginalFilename()));
}
controller写好后还需要去spring-servlet.xml文件中去配置,注意name必须用multipartResolver,不能修改,可以设置文件的最大的大小等
<!-- 配置文件上传的处理器 -->
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"></property>
</bean>
上传成功
如果要上传多个文件也可以轻松实现,取不同的名字就可以了
2.文件下载
一、通用下载方式
@RequestMapping("/download")
public void download(HttpServletRequest request,HttpServletResponse response) throws IOException {
File file=new File("c:/test/01.jpg");
InputStream in=new FileInputStream(file);
//设置响应头部
response.setCharacterEncoding("utf-8");//设置编码方式
response.setContentType("multipart/form-data");//设置响应类型,就是告诉它下载的内容是什么
//attachment告诉它已窗口的方式打开
response.setHeader("Content-Disposition", "attachment;fileName="+file.getName() );
//HttpServletResponse中的字节输出流,用于给用户输出,用户提交一个请求给你,你可以响应给他一个页面、文字、文件、io流
//现在有一个out就很简单了,要下载什么东西,直接往out里面去写就好了
ServletOutputStream out = response.getOutputStream();
//文件复制
int num = 0;
byte[] b = new byte[1024];
while ((num = in.read(b)) != -1) {
out.write(b, 0, num);
}
out.flush();
out.close();
in.close();
}
二、基于ResponseEntity
@RequestMapping("/download2")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException{
//需要下载的文件
File file=new File("c:/test/01.jpg");
byte[] body=null;
InputStream is=new FileInputStream(file);
body=new byte[is.available()];//is.available()获取有效长度
is.read(body);//一次性读取,把整个文件一次性放进body数组
HttpHeaders headers=new HttpHeaders();//头部信息
headers.add("Content-Disposition", "attachment;fileName="+file.getName());
HttpStatus statusCode=HttpStatus.OK;//设置响应状态
ResponseEntity<byte[]> entity=new ResponseEntity<byte[]>(body,headers,statusCode);
return entity;
}