使用 springMVC提供的CommonsMultipartFile类读取文件 1. 在spring-mvc中配置文件上传解析器 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <!-- 上传文件的最大值 --> <property name="maxUploadSize" value="10485760000"></property> <!-- 缓存大小 --> <property name="maxInMemorySize" value="40960"></property> </bean> spring-mvc.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:mvc="http://www./schema/mvc" xmlns:p="http://www./schema/p" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc-3.2.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd"> <!-- 静态资源访问 :正常所有的请求都会拦截,但resources中的资源直接访问,不要拦截了,**代表旗下的所有文件--> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <!-- 注解扫描包,自动扫描其下的所有controller,用注解写的文件都会都会@controller --> <context:component-scan base-package="com.tag.web.controller.annotation"></context:component-scan> <!-- 开启注解 ,需要如下两个类--> <mvc:annotation-driven></mvc:annotation-driven> <!-- 上传文件配置解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <!-- 上传文件的最大值 --> <property name="maxUploadSize" value="10485760000"></property> <!-- 缓存大小 --> <property name="maxInMemorySize" value="40960"></property> </bean> <!-- 视图解析器 以来的包是 InternalResourceViewResolver--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans> ---------------------------------------------------------------------------------------------------------------- upload.jsp文件 从这个页面发出上传请求 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <%@ taglib uri="http://java./jstl/core_rt" prefix="c"%> <% 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 'welcome.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"> --> <script type="text/javascript" src="/springMVC6/js/jquery-2.0.0.min.js"></script> <script type="text/javascript"> </script> </head> <body> <h>上传文件</h> <!-- 文件上传必须选择post --> <form action="/springMVC7/file/upload2" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file" /> <input type=submit value="添加" onclick="addUser();"/> </form> </body> </html> ---------------------------------------------------------------------------------------------------------------- success.jsp文件 文件上传成功后返回success.jsp页面 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <%@ taglib uri="http://java./jstl/core_rt" prefix="c"%> <% 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 'welcome.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"> --> <script type="text/javascript" src="/springMVC6/js/jquery-2.0.0.min.js"></script> <script type="text/javascript"> </script> </head> <body> <h>上传文件</h> <!-- 文件上传必须选择post --> <form action="/springMVC7/file/upload2" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file" /> <input type=submit value="添加" onclick="addUser();"/> </form> </body> </html> ---------------------------------------------------------------------------------------------------------------- UploadController.java类 package com.tag.web.controller.annotation.upload; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; 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(value="/file") public class UploadController { /** * @RequestParam("file") CommonsMultipartResolver file * 用CommonsMultipartResolver类去解析file上传的文件 * @param request * @param file * @return * @throws IOException */ @RequestMapping(value="/upload") public String upload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException{ //输出文件类型的名称 System.out.println("文件名:"+file.getOriginalFilename()); if(!file.isEmpty()){ //定义输出流,将文件保存到D盘根目录 FileOutputStream os = new FileOutputStream("D:/" +new Date().getTime()+ file.getOriginalFilename()); //文件输入流 InputStream in = file.getInputStream(); int b = 0; //读文件,并且文件没有读到尾部 while((b = in.read()) !=-1 ){ os.write(b); } os.flush(); os.close(); in.close(); } return "/success"; } @RequestMapping(value="/toUpload") public String toUpload(){ return "/upload"; } } ---------------------------------------------------------------------------------------------------------------- 在页面发起请求 http://localhost/springMVC7/file/toUpload |
|
来自: I_T_馆 > 《Spring MVC》