Skip to content

Commit eb4b0ac

Browse files
committed
add springmvc upload
1 parent 3f7fe9d commit eb4b0ac

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<module>nexus</module>
2929
<module>apache-poi</module>
3030
<module>java-compile</module>
31+
<module>SpringMVC_FileUpload</module>
32+
<module>springMVCFileUpload</module>
3133
</modules>
3234

3335
<name>learn-java-bug</name>

springMVCFileUpload/pom.xml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.nn</groupId>
8+
<artifactId>springmvc-fileUpload</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
11+
12+
<name>springmvc-fileUpload Maven Webapp</name>
13+
<!-- FIXME change it to the project's website -->
14+
<url>http://www.example.com</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.11</version>
27+
<scope>test</scope>
28+
</dependency>
29+
30+
<!--文件上传依赖-->
31+
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
32+
<dependency>
33+
<groupId>commons-fileupload</groupId>
34+
<artifactId>commons-fileupload</artifactId>
35+
<version>1.3.3</version>
36+
</dependency>
37+
38+
39+
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
40+
<dependency>
41+
<groupId>commons-io</groupId>
42+
<artifactId>commons-io</artifactId>
43+
<version>2.4</version>
44+
</dependency>
45+
<!--文件上传依赖-->
46+
47+
<!--spring依赖-->
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-context</artifactId>
51+
<version>4.3.1.RELEASE</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework</groupId>
55+
<artifactId>spring-test</artifactId>
56+
<version>4.2.5.RELEASE</version>
57+
</dependency>
58+
<!--springMVC依赖-->
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-web</artifactId>
62+
<version>4.3.1.RELEASE</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework</groupId>
66+
<artifactId>spring-webmvc</artifactId>
67+
<version>4.3.1.RELEASE</version>
68+
</dependency>
69+
70+
<!--springMVC依赖结束-->
71+
</dependencies>
72+
73+
<build>
74+
<finalName>springmvc-fileUpload</finalName>
75+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
76+
<plugins>
77+
<plugin>
78+
<artifactId>maven-clean-plugin</artifactId>
79+
<version>3.1.0</version>
80+
</plugin>
81+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
82+
<plugin>
83+
<artifactId>maven-resources-plugin</artifactId>
84+
<version>3.0.2</version>
85+
</plugin>
86+
<plugin>
87+
<artifactId>maven-compiler-plugin</artifactId>
88+
<version>3.8.0</version>
89+
</plugin>
90+
<plugin>
91+
<artifactId>maven-surefire-plugin</artifactId>
92+
<version>2.22.1</version>
93+
</plugin>
94+
<plugin>
95+
<artifactId>maven-war-plugin</artifactId>
96+
<version>3.2.2</version>
97+
</plugin>
98+
<plugin>
99+
<artifactId>maven-install-plugin</artifactId>
100+
<version>2.5.2</version>
101+
</plugin>
102+
<plugin>
103+
<artifactId>maven-deploy-plugin</artifactId>
104+
<version>2.8.2</version>
105+
</plugin>
106+
</plugins>
107+
</pluginManagement>
108+
</build>
109+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.nn.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.multipart.MultipartFile;
7+
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.OutputStream;
12+
13+
@Controller
14+
public class FileUploadController {
15+
16+
@RequestMapping("upload")
17+
public String upload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException {
18+
System.out.println("文件描述:"+ desc);
19+
20+
// 获取到 文件输入流
21+
InputStream inputStream = file.getInputStream();
22+
String originalFilename = file.getOriginalFilename(); // 获取 上传文件的原始 名称
23+
OutputStream outputStream = new FileOutputStream(originalFilename);
24+
byte[] buff = new byte[1024];
25+
int len = -1;
26+
while ((len = inputStream.read(buff))!=-1){
27+
outputStream.write(buff,0,len);
28+
}
29+
inputStream.close();
30+
outputStream.close();
31+
32+
System.out.println("上传成功");
33+
34+
return "success";
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
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">
6+
7+
8+
<!-- 开启注解扫描-->
9+
<context:component-scan base-package="com.nn"></context:component-scan>
10+
11+
<!--配置师徒解析器-->
12+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
13+
<!-- 前缀-->
14+
<property name="prefix" value="/view/"></property>
15+
16+
<!-- 后缀-->
17+
<property name="suffix" value=".jsp"></property>
18+
</bean>
19+
20+
<!-- 配置文件上传解析器
21+
id 值 固定为:multipartResolver
22+
-->
23+
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
24+
<!--配置编码-->
25+
<property name="defaultEncoding" value="utf-8"></property>
26+
27+
<!-- 上传单个文件的最大值,单位 byte ; 如果为: -1,表示无限制-->
28+
<property name="maxUploadSize" value="-1"></property>
29+
</bean>
30+
31+
</beans>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5+
version="4.0">
6+
7+
<!-- 配置 springMVC 入口-->
8+
<servlet>
9+
<servlet-name>dispatcherServlet</servlet-name>
10+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11+
<init-param>
12+
<param-name>contextConfigLocation</param-name>
13+
<param-value>classpath:springmvc.xml</param-value>
14+
</init-param>
15+
</servlet>
16+
<servlet-mapping>
17+
<servlet-name>dispatcherServlet</servlet-name>
18+
<url-pattern>/</url-pattern>
19+
</servlet-mapping>
20+
21+
22+
</web-app>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Hello World!</title>
9+
<meta name="viewport"
10+
content="width=device-width,user-scalable=no,maximum-scale=1.0,minimum-scale=1.0, initial-scale=1.0">
11+
</head>
12+
<body>
13+
<h2>文件长传</h2>
14+
15+
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data" >
16+
文件描述:<input name="desc" type="text"/>
17+
<input type="file" name="file">
18+
<input type="submit" value="上传">
19+
</form>
20+
</body>
21+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Hello World!</title>
9+
<meta name="viewport"
10+
content="width=device-width,user-scalable=no,maximum-scale=1.0,minimum-scale=1.0, initial-scale=1.0">
11+
</head>
12+
<body>
13+
<h2>success</h2>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)