2. SpringMVC 上传文件操作

时间:2024-01-13 20:24:50

1.创建java web项目:SpringMVCUploadDownFile

2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件

 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.httpclient-3.1.0.jar
com.springsource.org.apache.commons.lang-2.4.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.codehaus.jackson.mapper-1.0.0.jar
commons-dbcp.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
jotm.jar
ojdbc14.jar
org.springframework.aop-3.0.1.RELEASE-A.jar
org.springframework.asm-3.0.1.RELEASE-A.jar
org.springframework.aspects-3.0.1.RELEASE-A.jar
org.springframework.beans-3.0.1.RELEASE-A.jar
org.springframework.context-3.0.1.RELEASE-A.jar
org.springframework.core-3.0.1.RELEASE-A.jar
org.springframework.expression-3.0.1.RELEASE-A.jar
org.springframework.instrument-3.0.1.RELEASE-A.jar
org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar
org.springframework.jdbc-3.0.1.RELEASE-A.jar
org.springframework.orm-3.0.1.RELEASE-A.jar
org.springframework.oxm-3.0.1.RELEASE-A.jar
org.springframework.transaction-3.0.1.RELEASE-A.jar
org.springframework.web-3.0.1.RELEASE-A.jar
org.springframework.web.portlet-3.0.1.RELEASE-A.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
org.springframework.web.struts-3.0.1.RELEASE-A.jar
persistence.jar
xapool.jar

项目中所需要的jar

3.在项目的WebRoot下的WEB-INF下添加springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
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.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.xsd
">
<!-- 一。SpringMVC链接数据库步骤 -->
<!-- 1.mvc支持注解 -->
<mvc:annotation-driven/>
<!-- 2.全局扫描包 -->
<context:component-scan base-package="com"/> <!-- 3.驱动管理数据源 -->
<!-- 4.数据源事务管理 -->
<!-- 5.sqlsessionfactorybean -->
<!-- 6.spring和mybatis整合,映射输入参数 --> <!--二。 上传下载配置 --> <!-- 1.springMVC上传文件时,需要配置CommonsMultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置默认的编码格式 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总的大小不能超过200kb,
注意maxUploadSize属性点 限制 不是针对单个文件,
而是所有文件的容量总和-->
<property name="maxUploadSize" value="-1"/>
</bean> <!-- 2.springMVC在超出上传文件限制时,会抛出
org.springframework.web.multipart.MaxUploadSizeExceededException
该异常时SpringMVC在检查上传文件信息时抛出来的,而且此时还没有进入到Controller方法中
-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 在遇到MaxUploadSizeExceededException异常时,自动跳到xxx面 -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error.jsp</prop>
</props>
</property>
</bean> </beans>

springmvc-servlet.xml

4.在项目的WebRoot下添加index.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>上传文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<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">
-->
</head> <body>
<form action="<%=basePath%>upload.do" method="post" enctype="multipart/form-data">
<input type="hidden" name="holly" value="holly"/>
上传文件:<input type="file" name="uploadfile">
<input type="submit" value="上传">
</form>
</body>
</html>

index.jsp

5.在项目的WebRoot下添加success.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
-->
</head> <body>
文件上传成功
</body>
</html>

success.jsp

6.在项目的WebRoot下添加fail.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
-->
</head> <body>
文件上传失败
</body>
</html>

fail.jsp

7.在项目的WebRoot下添加error.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
-->
</head> <body>
文件上传异常
</body>
</html>

error.jsp

8.在项目的WebRoot下添加upload文件夹

9.在项目的src下的com.controller包下创建FileController.java文件

 package com.controller;

 import java.io.File;
import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils;
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.commons.CommonsMultipartFile; @Controller
public class FileController {
/**
*
* @param file 该属性使用得使用commons-io-2.0.jar文件,低版本不行
* @param request
* @return
*/
@RequestMapping("/upload.do")
public String queryFileData(@RequestParam("uploadfile") CommonsMultipartFile file,HttpServletRequest request){ /*MultipartFile是对当前上传的文件的封装,
* 当同时上传多个文件时,可以给定多个MultipartFile参数(数组)
*/
if(file!=null){
System.out.println("文件对象接到了!"); //获取文件名
String filename=file.getOriginalFilename();
System.out.println("filename:"+filename); //获取上传路径
String path=request.getSession().getServletContext().getRealPath("/upload/"+filename);
System.out.println("path:"+path); //创建文件流并指定写入路径
File destFile=new File(path); //springmvc的方式
//该方法自动操作,不需要额外的去关闭IO流
//复制临时文件到指定文件夹下
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
System.out.println("上传成功");
return "/success.jsp";
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败");
return "/error.jsp";
}
}else{
System.out.println("文件没有对象接到了!");
return "/error.jsp"; }
} }

FileController.java

10.有点困,结果自己测试肯定成功