Skip to content

Commit 2031ec3

Browse files
committed
spring mvc jquyer file upload
spring mvc jquyer file upload
1 parent 5955e97 commit 2031ec3

File tree

17 files changed

+8563
-0
lines changed

17 files changed

+8563
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.1.0.RELEASE/spring-core-3.1.0.RELEASE.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-asm/3.1.0.RELEASE/spring-asm-3.1.0.RELEASE.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-web/3.1.0.RELEASE/spring-web-3.1.0.RELEASE.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.1.0.RELEASE/spring-beans-3.1.0.RELEASE.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.1.0.RELEASE/spring-context-3.1.0.RELEASE.jar"/>
15+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.1.0.RELEASE/spring-aop-3.1.0.RELEASE.jar"/>
16+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.1.0.RELEASE/spring-expression-3.1.0.RELEASE.jar"/>
17+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-webmvc/3.1.0.RELEASE/spring-webmvc-3.1.0.RELEASE.jar"/>
18+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.1.0.RELEASE/spring-context-support-3.1.0.RELEASE.jar"/>
19+
<classpathentry kind="var" path="M2_REPO/commons-fileupload/commons-fileupload/1.2/commons-fileupload-1.2.jar"/>
20+
<classpathentry kind="var" path="M2_REPO/commons-io/commons-io/2.4/commons-io-2.4.jar"/>
21+
<classpathentry kind="var" path="M2_REPO/org/codehaus/jackson/jackson-mapper-asl/1.9.12/jackson-mapper-asl-1.9.12.jar"/>
22+
<classpathentry kind="var" path="M2_REPO/org/codehaus/jackson/jackson-core-asl/1.9.12/jackson-core-asl-1.9.12.jar"/>
23+
<classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar"/>
24+
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>
25+
</classpath>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.hmkcode</groupId>
5+
<artifactId>spring-mvc-jquery-file-upload</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>spring-mvc-jquery-file-upload Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<spring.version>3.1.0.RELEASE</spring.version>
13+
<junit.version>4.11</junit.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework</groupId>
19+
<artifactId>spring-core</artifactId>
20+
<version>${spring.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework</groupId>
24+
<artifactId>spring-web</artifactId>
25+
<version>${spring.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-webmvc</artifactId>
30+
<version>${spring.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>javax.servlet</groupId>
34+
<artifactId>servlet-api</artifactId>
35+
<version>2.5</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>commons-fileupload</groupId>
39+
<artifactId>commons-fileupload</artifactId>
40+
<version>1.2</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>commons-io</groupId>
44+
<artifactId>commons-io</artifactId>
45+
<version>2.4</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.codehaus.jackson</groupId>
49+
<artifactId>jackson-mapper-asl</artifactId>
50+
<version>1.9.12</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.codehaus.jackson</groupId>
54+
<artifactId>jackson-core-asl</artifactId>
55+
<version>1.9.12</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>${junit.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<finalName>spring-mvc-jquery-file-upload</finalName>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.mortbay.jetty</groupId>
71+
<artifactId>maven-jetty-plugin</artifactId>
72+
<version>6.1.10</version>
73+
<configuration>
74+
<scanIntervalSeconds>10</scanIntervalSeconds>
75+
</configuration>
76+
</plugin>
77+
<plugin>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<configuration>
80+
<source>1.6</source>
81+
<target>1.6</target>
82+
</configuration>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.hmkcode.spring.mvc.controllers;
2+
3+
import java.io.IOException;
4+
import java.util.Iterator;
5+
import java.util.LinkedList;
6+
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.util.FileCopyUtils;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.ResponseBody;
15+
import org.springframework.web.multipart.MultipartFile;
16+
import org.springframework.web.multipart.MultipartHttpServletRequest;
17+
18+
import com.hmkcode.spring.mvc.model.FileMeta;
19+
20+
@Controller
21+
@RequestMapping("/controller")
22+
public class FileController {
23+
24+
25+
LinkedList<FileMeta> files = new LinkedList<FileMeta>();
26+
FileMeta fileMeta = null;
27+
/***************************************************
28+
* URL: /rest/controller/upload
29+
* upload(): receives files
30+
* @param request : MultipartHttpServletRequest auto passed
31+
* @param response : HttpServletResponse auto passed
32+
* @return LinkedList<FileMeta> as json format
33+
****************************************************/
34+
@RequestMapping(value="/upload", method = RequestMethod.POST)
35+
public @ResponseBody LinkedList<FileMeta> upload(MultipartHttpServletRequest request, HttpServletResponse response) {
36+
37+
//1. build an iterator
38+
Iterator<String> itr = request.getFileNames();
39+
MultipartFile mpf = null;
40+
41+
//2. get each file
42+
while(itr.hasNext()){
43+
44+
//2.1 get next MultipartFile
45+
mpf = request.getFile(itr.next());
46+
System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size());
47+
48+
//2.2 if files > 10 remove the first from the list
49+
if(files.size() >= 10)
50+
files.pop();
51+
52+
//2.3 create new fileMeta
53+
fileMeta = new FileMeta();
54+
fileMeta.setFileName(mpf.getOriginalFilename());
55+
fileMeta.setFileSize(mpf.getSize()/1024+" Kb");
56+
fileMeta.setFileType(mpf.getContentType());
57+
58+
try {
59+
fileMeta.setBytes(mpf.getBytes());
60+
} catch (IOException e) {
61+
// TODO Auto-generated catch block
62+
e.printStackTrace();
63+
}
64+
//2.4 add to files
65+
files.add(fileMeta);
66+
67+
}
68+
69+
// result will be like this
70+
// [{"fileName":"app_engine-85x77.png","fileSize":"8 Kb","fileType":"image/png"},...]
71+
return files;
72+
73+
}
74+
/***************************************************
75+
* URL: /rest/controller/get/{value}
76+
* get(): get file as an attachment
77+
* @param response : passed by the server
78+
* @param value : value from the URL
79+
* @return void
80+
****************************************************/
81+
@RequestMapping(value = "/get/{value}", method = RequestMethod.GET)
82+
public void get(HttpServletResponse response,@PathVariable String value){
83+
FileMeta getFile = files.get(Integer.parseInt(value));
84+
try {
85+
response.setContentType(getFile.getFileType());
86+
response.setHeader("Content-disposition", "attachment; filename=\""+getFile.getFileName()+"\"");
87+
FileCopyUtils.copy(getFile.getBytes(), response.getOutputStream());
88+
}catch (IOException e) {
89+
// TODO Auto-generated catch block
90+
e.printStackTrace();
91+
}
92+
}
93+
94+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hmkcode.spring.mvc.model;
2+
3+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
4+
5+
@JsonIgnoreProperties({"bytes"})
6+
public class FileMeta {
7+
8+
private String fileName;
9+
private String fileSize;
10+
private String fileType;
11+
12+
private byte[] bytes;
13+
14+
public String getFileName() {
15+
return fileName;
16+
}
17+
public void setFileName(String fileName) {
18+
this.fileName = fileName;
19+
}
20+
public String getFileSize() {
21+
return fileSize;
22+
}
23+
public void setFileSize(String fileSize) {
24+
this.fileSize = fileSize;
25+
}
26+
public String getFileType() {
27+
return fileType;
28+
}
29+
public void setFileType(String fileType) {
30+
this.fileType = fileType;
31+
}
32+
public byte[] getBytes() {
33+
return bytes;
34+
}
35+
public void setBytes(byte[] bytes) {
36+
this.bytes = bytes;
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:mvc="http://www.springframework.org/schema/mvc"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-3.0.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
12+
13+
<context:component-scan base-package="com.hmkcode.spring.mvc.controllers" />
14+
<mvc:annotation-driven />
15+
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
16+
</beans>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6+
id="WebApp_ID" version="2.5">
7+
<display-name>Archetype Created Web Application</display-name>
8+
<welcome-file-list>
9+
<welcome-file>index.html</welcome-file>
10+
</welcome-file-list>
11+
12+
<servlet>
13+
<servlet-name>rest</servlet-name>
14+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
15+
</servlet>
16+
<servlet-mapping>
17+
<servlet-name>rest</servlet-name>
18+
<url-pattern>/rest/*</url-pattern>
19+
</servlet-mapping>
20+
</web-app>

0 commit comments

Comments
 (0)