2. SpringMVC 上传文件操作
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.有点困,结果自己测试肯定成功
2. SpringMVC 上传文件操作的更多相关文章
- SpringMVC上传文件总结
如果是maven项目 需要在pom.xml文件里面引入下面两个jar包 <dependency> <groupId>commons-fileupload</groupId ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- springmvc上传文件,抄别人的
SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...
- 使用springMVC上传文件
control层实现功能: @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...
- SpringMVC上传文件(图片)并保存到本地
SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...
- SpringMVC 上传文件 MultipartFile 转为 File
在使用 SpringMVC 上传文件时,接收到的文件格式为 MultipartFile,但是在很多场景下使用都需要File格式的文件,记录下以便日后使用. 以下mFile为MultipartFile文 ...
- springmvc 上传文件时的错误
使用springmvc上传文件一直失败,文件参数一直为null, 原来是配置文件没写成功. <bean id="multipartResolver" class=" ...
- SpringMVC上传文件
SpringMVC中上传文件还是比较方便的,Spring内置了一些上传文件的支持类,不需要复杂的操作即可上传文件. 文件上传需要两个jar支持,一个是commons-fileupload.jar和co ...
- SpringMVC上传文件的三种方式(转)
直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/common ...
随机推荐
- Android调用本机应用市场,实现应用评分功能
原本以为应用评分是个很小的功能,但是一实现才发现真不是个小事.网上搜索资料没有找到答案,在很多开发群里面询问了很多人也没有解决问题,最后分析log,反编译看源码才终于有了些眉目,好吧,上代码: try ...
- 目前比较流行的Python科学计算发行版
经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...
- async/task/await
async/task/await三组合是.NET Framework 4.5带给.NET开发者的大礼,合理地使用它,可以提高应用程序的吞吐能力. 但是它的使用有点绕人,如果不正确使用,会带来意想不到的 ...
- C#控件怎样获取,和失去焦点的处理
publicForm1() { InitializeComponent(); textBox1.Enter+=newEventHandler(textBox1_Enter);//获得焦点事件 text ...
- Binder机制,从Java到C (10. Binder驱动)
Binder驱动的代码都在kernel里面,这里就简单讲一下里面涉及到的几个东西: 1.MemoryBinder其实本质上就是一中数据传输方式,这种方式是通过binder driver实现的. 我们知 ...
- 监听JVM关闭
使用Runtime的addShutdownHook(thread)方法: for(int i=0; i<5; i++){ System.out.println(i); } Thread th = ...
- Linux Shell脚本攻略
-Linux Shell脚本攻略 总结的来说,这本书很实践性和实用性强,都是给的具体的例子,直接可以在终端操作实践,比单纯只看不动手务实多了,另外就是,这本书涵盖的内容也比较广,从文本操作到服务器管理 ...
- 一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS.
一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大( ...
- session验证登陆- 页面跳转
用session验证登陆,当用户想访问一个页面时由于没有登录,就跳转到登录页面,登录后跳转到用户请求的页面,在session跳转中传上次请求的页面. 怎么获得这个url值并跳转到该页面呢? 以此跳转 ...
- OAuth的一个.NET开源实现
从编译DotNetOpenAuth中学到的程序集强签名知识 OAuth的一个.NET开源实现,官方网站:http://dotnetopenauth.net/ . 从GitHub签出DotNetOpen ...