springMVC系列之文件上传——06
springMVC系列之文件上传——06
摘要:web开发、文件上传就像我们平时吃饭一样、少不了的功能。本文主要实现springMVC的两种实现方式:通过参数传递上传文件、通过解析request上传文件、强推后面一种。
一:实现功能
1、通过参数传递形式上传文件
2、通过解析request上传文件
二:具体步骤
2、不管是那种方式、首先要在springMVC的配置文件中配置有管springMVC对上传文件的处理的类springAnnotation-servlet.xml关于文件处理配置:
<!-- 配置springMVC处理上传文件的信息 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean>
3、参数传递(详细信息代码有说明):
/** * 参数接收形式上传文件 */ @RequestMapping("/upload") public String uploadFile(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { System.out.println("fileName : " + file.getOriginalFilename()); if (!file.isEmpty()) { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("D:/" + file.getOriginalFilename())); InputStream in = file.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); int n = 0; byte[] b = new byte[1024]; while ((n = bis.read(b)) != -1) { bos.write(b, 0, n); } bos.flush(); bos.close(); bis.close(); } return "/success"; }
4、解析request(详细信息代码有说明):
/** * springMVC封装的解析request上传文件(快捷、与springMVC很好结合,首选) */ @RequestMapping("/upload2") public String uploadFile2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { // 定义解析器去解析request CommonsMultipartResolver mutilpartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //request如果是Multipart类型、 if (mutilpartResolver.isMultipart(request)) { //强转成 MultipartHttpServletRequest MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; //获取文件名 Iterator<String> it = multiRequest.getFileNames(); while (it.hasNext()) { //获取MultipartFile类型文件 MultipartFile fileDetail = multiRequest.getFile(it.next()); if (fileDetail != null) { String fileName = "demoUpload" + fileDetail.getOriginalFilename(); String path = "D:/" + fileName; File localFile = new File(path); //将上传文件写入到指定文件出、核心! fileDetail.transferTo(localFile); //非常重要、有了这个想做什么处理都可以 //fileDetail.getInputStream(); } } } return "/success"; }
补充
1、整体结构图及jar包图
2、具体页面、Controller、springAnnotation-servlet.xml(嫌烦可无视)
a) 上传页面 upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>上传文件</title> <script type="text/javascript"> </script> </head> <body> <h4>上传文件1</h4> <form name="userForm" action="/springMVC7/file/upload2" method="post" enctype="multipart/form-data" > 选择文件:<input type="file" name="file"> <input type="submit" value="上传" > </form> </body> </html>
b)配置文件 springAnnotation-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.chy.web.controller.annotation"></context:component-scan> <!-- 优化一:使用mvc标签的代替下面的两个bean --> <mvc:annotation-driven /> <!-- 不拦截静态资源 --> <mvc:default-servlet-handler /> <!-- 开启注解 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置springMVC处理上传文件的信息 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> </beans>
c)处理上传文件的类 UploadController
package com.chy.web.controller.annotation.upload; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; 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 org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartResolver; @Controller @RequestMapping("/file") public class UploadController { /* * 上传文件的两种方法、 1、一种用参数接收 2、解析request */ /** * 参数接收形式上传文件 */ @RequestMapping("/upload") public String uploadFile(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { System.out.println("fileName : " + file.getOriginalFilename()); if (!file.isEmpty()) { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("D:/" + file.getOriginalFilename())); InputStream in = file.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); int n = 0; byte[] b = new byte[1024]; while ((n = bis.read(b)) != -1) { bos.write(b, 0, n); } bos.flush(); bos.close(); bis.close(); } return "/success"; } /** * springMVC封装的解析request上传文件(快捷、与springMVC很好结合,首选) */ @RequestMapping("/upload2") public String uploadFile2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { // 定义解析器去解析request CommonsMultipartResolver mutilpartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //request如果是Multipart类型、 if (mutilpartResolver.isMultipart(request)) { //强转成 MultipartHttpServletRequest MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; //获取文件名 Iterator<String> it = multiRequest.getFileNames(); while (it.hasNext()) { //获取MultipartFile类型文件 MultipartFile fileDetail = multiRequest.getFile(it.next()); if (fileDetail != null) { String fileName = "demoUpload" + fileDetail.getOriginalFilename(); String path = "D:/" + fileName; File localFile = new File(path); //将上传文件写入到指定文件出、核心! fileDetail.transferTo(localFile); //非常重要、有了这个想做什么处理都可以 //fileDetail.getInputStream(); } } } return "/success"; } @RequestMapping("toUpload") public String tpUpload() { return "/upload"; } }
更多内容: springMVC系列之目录——00
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。