了解SpringMVC的上传和下载
作者:wbcra
今天小编就为大家分享一篇关于Spring整合Springmvc的相关介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
springmvc.xml的配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 开启ioc注解开启扫描--> <context:component-scan base-package="cn.hp"></context:component-scan> <!-- 开启mvc的注解扫描 里面有个转换服务器--> <mvc:annotation-driven ></mvc:annotation-driven> <!-- 将视图解析器 交给spring--> <bean id="resolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- <property name="prefix" value="/WEB-INF/pages/"></property>--> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置一个bean 让他支持文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 俩属性 name="defaultEncoding" 编码格式utf-8 name="maxUploadSize" 文件上传最大 值 100M --> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="10240000"></property> </bean> </beans>
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化时加载springmvc配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>fileEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>Encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>fileEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
主要代码
package cn.hp.controller; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Random; @Controller public class FileUploadController { @RequestMapping(value="upload.do") public String upload(MultipartFile file[], HttpServletRequest request) throws IOException { //1.获取上传文件路径 String path = request.getServletContext().getRealPath("/upload"); for (int i =0;i<file.length;i++) { //2.拿到上传的文件名 String name = file[i].getOriginalFilename(); //3.改名,把用户上传的文件进行改名操作 String newNmae = new Date().getTime() + new Random().nextInt(999999) + name; //4.实例化File类的对象加载上传的路径和文件 File f = new File(path + newNmae); //5. MultipartFile里面的方法把这个路径的文件写入过去 file[i].transferTo(f); } return "success"; } @RequestMapping(value="download.do") public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException { //1.下载路径 String path = request.getServletContext().getRealPath("/download/"); //2.实例化File类的对象加载下载的路径和文件 File f= new File(path+fileName); //3.转格式 String newName = new String(fileName.getBytes("utf-8"),"iso8859-1"); //4.转流 HttpHeaders hh = new HttpHeaders(); hh.setContentDispositionFormData("attachment",newName); hh.setContentType(MediaType.APPLICATION_OCTET_STREAM); //5.相应发送 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED); } }
NewFile.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--如果说表单有文件上传的功能,那么表达就要设置这个属性--%> <form action="upload" method="post" enctype="multipart/form-data"> 文件上传<input type="file" name="file" /><br/> 文件上传<input type="file" name="file" /><br/> 文件上传<input type="file" name="file" /><br/> <input type="submit" value="确定" /> </form> <a href="download/自我介绍模板.docx">下载文件</a> <a href="download/10.png">下载文件</a> <a href="download/4.jpg">下载文件1</a> <a href="download.do?fileName=新建文本文档.txt">下载文件2</a> <a href="download/新建文本文档.txt">下载文件3</a> </body> </html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 上传成功 </body> </html>
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!