1. establish java web project :SpringMVCUploadDownFile

2. In the project WebRoot Under the WEB-INF Of lib Add the following jar file

 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

What's needed in the project jar

3. In the project WebRoot Under the WEB-INF Add below springmvc-servlet.xml file

 <?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
">
<!-- One .SpringMVC Link database steps -->
<!-- 1.mvc Support annotation -->
<mvc:annotation-driven/>
<!-- 2. Global scan package -->
<context:component-scan base-package="com"/> <!-- 3. Driver management data source -->
<!-- 4. Data source transaction management -->
<!-- 5.sqlsessionfactorybean -->
<!-- 6.spring and mybatis Integrate , Map input parameters --> <!-- Two . Upload and download configuration --> <!-- 1.springMVC When uploading files , Need configuration CommonsMultipartResolver processor -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Set the default encoding format -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- Specifies that the total size of the uploaded file cannot exceed 200kb,
Be careful maxUploadSize Attribute point Limit Not for a single file ,
It's the sum of all the files -->
<property name="maxUploadSize" value="-1"/>
</bean> <!-- 2.springMVC When the upload file limit is exceeded , Will throw out
org.springframework.web.multipart.MaxUploadSizeExceededException
When this exception occurs SpringMVC Thrown when checking the information of the uploaded file , And it's not even here Controller In the method
-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- In case of MaxUploadSizeExceededException When abnormal , Auto jump to xxx Noodles -->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error.jsp</prop>
</props>
</property>
</bean> </beans>

springmvc-servlet.xml

4. In the project WebRoot Add below index.jsp file

 <%@ 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> Upload files </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"/>
Upload files :<input type="file" name="uploadfile">
<input type="submit" value=" Upload ">
</form>
</body>
</html>

index.jsp

5. In the project WebRoot Add below success.jsp file

 <%@ 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>
File upload succeeded
</body>
</html>

success.jsp

6. In the project WebRoot Add below fail.jsp file

 <%@ 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>
File upload failed
</body>
</html>

fail.jsp

7. In the project WebRoot Add below error.jsp file

 <%@ 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>
File upload exception
</body>
</html>

error.jsp

8. In the project WebRoot Add below upload Folder

9. In the project src Under the com.controller Package created under FileController.java file

 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 This property uses commons-io-2.0.jar file , The lower version doesn't work
* @param request
* @return
*/
@RequestMapping("/upload.do")
public String queryFileData(@RequestParam("uploadfile") CommonsMultipartFile file,HttpServletRequest request){ /*MultipartFile Is the encapsulation of the currently uploaded file ,
* When uploading multiple files at the same time , You can give multiple MultipartFile Parameters ( Array )
*/
if(file!=null){
System.out.println(" File object received !"); // Get the file name
String filename=file.getOriginalFilename();
System.out.println("filename:"+filename); // Get the upload path
String path=request.getSession().getServletContext().getRealPath("/upload/"+filename);
System.out.println("path:"+path); // Create a file stream and specify the write path
File destFile=new File(path); //springmvc The way
// The method operates automatically , There is no need to close it IO flow
// Copy the temporary file to the specified folder
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
System.out.println(" Upload successful ");
return "/success.jsp";
} catch (IOException e) {
e.printStackTrace();
System.out.println(" Upload failed ");
return "/error.jsp";
}
}else{
System.out.println(" There is no object in the file received !");
return "/error.jsp"; }
} }

FileController.java

10. A little sleepy , The results of their own test must be successful

2. SpringMVC Upload file operation more related articles

  1. SpringMVC Upload file summary

    If it is maven project Need to be in pom.xml The following two are introduced into the file jar package <dependency> <groupId>commons-fileupload</groupId ...

  2. adopt cmd complete FTP Upload file operation

    Always use FileZilla This tool does related FTP operation , And after a version upgrade , It doesn't work very well , The connection keeps dropping , And then it couldn't connect at all . It's been a while Web Version of FTP Tools , Later that page also ...

  3. springmvc Upload files , Copy someone else's

    SpringMVC File upload in classification : SpringMVC 2012-05-17 12:55 26426 Human reading Comment on (13) Collection report stringuserinputclassencoding This is a ...

  4. Use springMVC Upload files

    control Layer implementation function : @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...

  5. SpringMVC Upload files ( picture ) And save to local

    SpringMVC Upload files ( picture ) And save to local A little note ~ Basic MVC Configuration doesn't show , Here's the core code stay spring-mvc Write the following configuration in the configuration file of <bean id="multi ...

  6. SpringMVC Upload files MultipartFile To File

    In the use of SpringMVC When uploading files , The received file format is MultipartFile, But in many scenarios, you need File File format , Write it down for future use . following mFile by MultipartFile writing ...

  7. SpringMVC Upload files

    SpringMVC It's more convenient to upload files in ,Spring Built in support classes for uploading files , No complicated operation is needed to upload files . File upload takes two jar Support , One is commons-fileupload.jar and co ...

  8. SpringMVC Three ways to upload files ( turn )

    Go straight to the code , You can see it at a glance At this time :commonsmultipartresolver Source code , You can study it  http://www.verysource.com/code/2337329_1/common ...

  9. SpringMVC Three ways to upload files

    Go straight to the code , You can see it at a glance At this time :commonsmultipartresolver Source code , You can study it  http://www.verysource.com/code/2337329_1/common ...

Random recommendation

  1. C# Timer component for developing system services

    When writing a service , Need to write a lot of code for the timer , It's a lot of trouble , Today, I'd like to encapsulate this code , I hope to simplify the work in this area , Focus on function This timer assembly , Only one service instance is started for processing at a time , Instead of executing the service code multiple times at the same time . ...

  2. javascript note ——setTimeout The parameter problem of

    setTimeout("xxx",500) Scopes in double quotes do not capture local variables , So it will report an error If you need to define a variable in double quotes var now; window.onload ...

  3. Java Study notes —— Inner class

    Inner class form : Define a class inside a class . It is divided into : Member inner class and anonymous inner class a)  Member inner class b)  Static member inner class c)  Anonymous inner class d)  Local inner classes (1) Member inner class : Java Our language is oriented to ...

  4. JAVA For beginners JDB Try

    Use JDB Debugging simple recursive programs Learn from teacher Lou's blog , First, use it on the terminal Ctrl+Shift+T Open three tabs , Convenient operation . Use Vim Edit your own program , The procedure is as follows 1 public class Factori ...

  5. About dynamic loading js

    We know that a js Array of file paths for , Dynamically load all the data in the array js file . There are two cases of loading : 1. Parallel loading , No matter js Execution order of . 2. Serial loading , That is, load one by one , Load the next one after the last one , Until the last . ...

  6. JVM Tools jcmd practice

    For now, please refer to the following links , Follow up to add their own practice . https://www.jianshu.com/p/388e35d8a09b

  7. Tween.js Animation effect

    One .apply, and call Usage of . Let's start with something that has nothing to do with this blog post , Namely apply and call Usage of . Actually apply and call The usage is the same , It's just that their biographies are different .apply It's an array , and call It's a separate biography , class ...

  8. opensips install

    See this tutorial for the main process : Thank you bosses http://blog.csdn.net/u011026329/article/details/50821679 among  mediaproxy There will be errors in the installation of . Make the following changes Next ...

  9. nc- Usage method

    nc- Remote clone hard disk A The receiver :  nc -lp 333 | dd of=/dev/sda          # use nc Turn on 333 Listening port   Data to be received Write to sda On ( The premise is to hang a hard disk ) B ...

  10. 【Leetcode】【Medium】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Problem solving : Determine whether a single linked list has a ring , Using two Pointers once and twice Traversing the linked list ,once One walk at a time ...