SpringMVC教程2[处理及响应请求]
一、文件上传
1.引入相关jar包
maven坐标
<!-- fileUpload 解析上传的文件用到的jar --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2.页面表单信息
表单提交方式必须是post方式提交,enctype必须是multipart/form-data
<form action="upload" method="post" enctype="multipart/form-data"> <table> <tr> <td>用户名</td> <td><input type="text" name="username"></td> </tr> <tr> <td>用户密码</td> <td><input type="password" name="password"></td> </tr> <tr> <td>用户图像</td> <td><input type="file" name="userface"></td> </tr> <tr> <td><input type="submit" value="注册"></td> </tr> </table> </form>
3.Controller中接收数据
/** * 文件上传案例 * @author dpb【波波烤鸭】 * */ @Controller public class UserController { /** * 上传的数据通过MultipartFile对象接收 * @param username * @param password * @param userface * @throws Exception * @throws IOException */ @RequestMapping("/upload") @ResponseBody public void upload(String username,String password ,MultipartFile userface) throws Exception, IOException{ System.out.println(username+","+password); userface.transferTo(new File("c:/tools/","123.png")); } }
4.配置文件中修改
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver" > <!-- 设置上传文件信息参数 --> <!-- 设置文件上传的最大尺寸 --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean>
二、文件下载
1.方式一:基于ResponseEntity实现
@RequestMapping("/testHttpMessageDown") public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException { // 需要下载的文件 File file = new File("E://123.jpg"); byte[] body = null; InputStream is = new FileInputStream(file); body = new byte[is.available()]; is.read(body); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attchement;filename=" + file.getName()); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode); return entity; }
方式二:通用下载实现
@RequestMapping("/exportExcel") public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws IOException{ File file = new File("d://owned.xls"); //设置响应头和客户端保存文件名 response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName()); try { //打开本地文件流 InputStream inputStream = new FileInputStream(file); //激活下载操作 OutputStream os = response.getOutputStream(); //循环写入输出流 byte[] b = new byte[2048]; int length; while ((length = inputStream.read(b)) > 0) { os.write(b, 0, length); } // 这里主要关闭。 os.close(); inputStream.close(); } catch (Exception e){ throw e; } }
三、静态资源处理
3.1 在web.xml中配置default servlet
<!-- 防止资源文件被spring MVC拦截 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping>
3.2在配置文件中通过标签设置
例如,浏览器发送http://localhost:8080/static/img/01.png请求,该请求符合/static/img/*,此时,*代表01.png,那么springmvc会将01.png补充到对应的location后面,进而查找到文件。
这里需要注意:
* 表示一层路径
** 表示多层路径映射