- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringMVC上传文件FileUpload使用方法详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了SpringMVC上传文件FileUpload的具体代码,供大家参考,具体内容如下 。
我是在已经搭建好的springMVC环境下,maven工程中的pom.xml所需要的jar包(其中spring上传文件的两个主要jar:commons-fileupload.jar和commons-io.jar):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
<project xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>
4.0
.
0
</modelVersion>
<groupId>com.zhihua</groupId>
<artifactId>SpringMVC_FileUpload</artifactId>
<packaging>war</packaging>
<version>
0.0
.
1
-SNAPSHOT</version>
<name>SpringMVC_FileUpload Maven Webapp</name>
<url>http:
//maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>
3.2
.
8
.RELEASE</spring.version>
<!-- junit版本号 -->
<junit.version>
4.10
</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>
3.8
.
1
</version>
<scope>test</scope>
</dependency>
<!-- springMVC上传文件依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>
1.3
.
1
</version>
</dependency>
<!-- 添加Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- jstl标签支持 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>
1.2
</version>
</dependency>
<!-- Servlet核心包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>
3.0
.
1
</version>
<scope>provided</scope>
</dependency>
<!--JSP -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>
2.1
</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC_FileUpload</finalName>
</build>
</project>
|
在spring-mvc.xml中添加代码:
1
2
3
4
5
6
7
|
<!-- springMVC上传文件时,需要配置MultipartResolver处理器 -->
<
bean
id
=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
<
property
name
=
"defaultEncoding"
value
=
"utf-8"
></
property
>
<
property
name
=
"maxUploadSize"
value
=
"10485760000"
></
property
>
<
property
name
=
"maxInMemorySize"
value
=
"40960"
></
property
>
</
bean
>
|
前端页面(fileUpload.jsp)和上传成功返回页面(success.jsp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>springmvc-文件上传</
title
>
</
head
>
<
body
>
<
h1
>采用流的方式上传文件</
h1
>
<
form
name
=
"form1"
action="<c:url
value
=
'/fileUpload/upload_01'
/>" method="post" enctype="multipart/form-data">
<
input
type
=
"file"
name
=
"file_01"
/>
<
input
type
=
"submit"
value
=
"fileupload"
/>
</
form
>
<
br
>
<
h1
>采用multipart提供的file.transfer方法上传文件1</
h1
>
<
form
name
=
"form2"
action="<c:url
value
=
'/fileUpload/upload_02'
/>" method="post" enctype="multipart/form-data">
<
input
type
=
"file"
name
=
"file_02"
/>
<
input
type
=
"submit"
value
=
"fileupload"
/>
</
form
>
<
br
>
<
h1
>采用multipart提供的file.transfer方法上传文件2</
h1
>
<
form
name
=
"form2"
action="<c:url
value
=
'/fileUpload/upload_03'
/>" method="post" enctype="multipart/form-data">
<
input
type
=
"file"
name
=
"file_03"
/>
<
input
type
=
"submit"
value
=
"fileupload"
/>
</
form
>
<
br
>
<
h1
>使用spring MVC提供的方法上传文件</
h1
>
<
form
name
=
"form2"
action="<c:url
value
=
'/fileUpload/upload_spring'
/>" method="post" enctype="multipart/form-data">
<
input
type
=
"file"
name
=
"file_03"
/>
<
input
type
=
"submit"
value
=
"fileupload"
/>
</
form
>
</
body
>
</
html
>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>Insert title here</
title
>
</
head
>
<
body
>
<
h2
>文件上传成功!</
h2
>
</
body
>
</
html
>
|
controller 的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
package
com.zhihua.controller;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.util.Date;
import
java.util.Iterator;
import
javax.servlet.http.HttpServletRequest;
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.MultipartHttpServletRequest;
import
org.springframework.web.multipart.commons.CommonsMultipartFile;
import
org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
@RequestMapping
(
"/fileUpload"
)
public
class
FileUploadController {
/**
* 通过流的方式上传文件
* @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param file
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月5日]
*/
@RequestMapping
(
"/upload_01"
)
public
String fileUpload_1(
@RequestParam
(
"file_01"
)CommonsMultipartFile file)
throws
IOException {
//用来检测程序运行时间
long
startTime = System.currentTimeMillis();
System.out.println(
"fileName:"
+file.getOriginalFilename());
try
{
//获取输出流
OutputStream os =
new
FileOutputStream(
"E:/"
+
new
Date().getTime()+file.getOriginalFilename());
//获取输入流CommonsMultipartFile中可以直接得到文件的流
InputStream is = file.getInputStream();
int
temp;
//一个一个字节的读取并写入
while
((temp=is.read())!=(-
1
)){
os.write(temp);
}
os.flush();
os.close();
is.close();
}
catch
(Exception e){
e.printStackTrace();
}
long
endTime = System.currentTimeMillis();
System.out.println(
"方法一的运行时间:"
+String.valueOf(endTime-startTime)+
"ms"
);
return
"view/success"
;
}
/**
* 采用file.Transto 来保存上传的文件
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param file
* @return
* @throws IOException
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月5日]
*/
@RequestMapping
(
"/upload_02"
)
public
String fileUpload_2(
@RequestParam
(
"file_02"
) CommonsMultipartFile file)
throws
IOException {
long
startTime = System.currentTimeMillis();
System.out.println(
"fileName:"
+file.getOriginalFilename());
String path =
"E:/"
+
new
Date().getTime()+file.getOriginalFilename();
File newFile =
new
File(path);
//通过CommonsMultipartFile 的方法直接写文件
file.transferTo(newFile);
long
endTime = System.currentTimeMillis();
System.out.println(
"方法二的运行时间:"
+String.valueOf(endTime-startTime)+
"ms"
);
return
"view/success"
;
}
/**
* 使用MultipartFile上传文件
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param file
* @return
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月5日]
*/
@RequestMapping
(
"/upload_03"
)
public
String fileUpload_3(
@RequestParam
(value=
"file_03"
,required=
false
)MultipartFile file,
HttpServletRequest request){
String path = request.getSession().getServletContext().getRealPath(
"upload"
);
System.out.println(path);
String fileName = file.getOriginalFilename();
File targetFile =
new
File(path,fileName);
if
(!targetFile.exists()){
targetFile.mkdirs();
}
//保存
try
{
file.transferTo(targetFile);
}
catch
(Exception e){
e.printStackTrace();
}
return
"view/success"
;
}
/**
* 采用spring提供的上传文件的方法
* <请替换成功能描述> <br>
* <请替换成详细描述>
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
* @author caizh
* @since [1.0.0]
* @version [1.0.0,2017年2月5日]
*/
@RequestMapping
(
"/upload_spring"
)
public
String springUpload(HttpServletRequest request)
throws
IllegalStateException, IOException{
long
startTime = System.currentTimeMillis();
//将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver =
new
CommonsMultipartResolver(request.getSession().getServletContext());
//检查form中是否有enctype="multipart/form-data"
if
(multipartResolver.isMultipart(request))
{
//将request变成多request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//获取multiRequest中所有的文件名
Iterator iter = multiRequest.getFileNames();
while
(iter.hasNext()){
//一次遍历所有的文件
MultipartFile file = multiRequest.getFile(iter.next().toString());
if
(file!=
null
){
String path =
"E:/springUpload"
+file.getOriginalFilename();
//上传
file.transferTo(
new
File(path));
}
}
}
long
endTime = System.currentTimeMillis();
System.out.println(
"方法三的运行时间:"
+String.valueOf(endTime-startTime)+
"ms"
);
return
"view/success"
;
}
}
|
好了,全部代码都给出了,博主是测试成功了,感兴趣的朋友可以去比较不同上传方式的效率! 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/hundan_520520/article/details/54880319 。
最后此篇关于SpringMVC上传文件FileUpload使用方法详解的文章就讲到这里了,如果你想了解更多关于SpringMVC上传文件FileUpload使用方法详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在配置 blueimp 以在单击发布按钮时上传图像。我的问题是它没有调用 $('#fileupload').fileupload 函数。我已经包含了以下 javascript 文件。以下是我的代码
我正在使用 Firebase 构建一个 Angular 应用程序,以将图像上传到数据库并从那里检索它。 上传文件.service.ts import {Injectable} from '@angul
我应该使用哪个库?唯一的目的是上传文件。在处理大型请求时,我想在性能和可扩展性方面使用最佳库。如果我的项目扩大规模,我最终将使用 CDN 服务。 最佳答案 以下是在这种情况下选择图书馆时必须考虑的一些
我尝试什么:我想使用 jQuery mobile 在 JSF 页面中上传带有 Primefaces FileUpload 标记的图片。此面仅在桌面浏览器上可用,但也应具有与其他页面一样的外观。问题:总
我在 gridview 中使用了 Ajax fileupload 控件,为 gridview 中的每条记录上传文件。我使用了 ajax fileupload 控件,因为我也需要拖放上传功能。它的工作很
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章FileUpload 控件禁止手动输入的方法由作者收集整理,如果你对这篇
我正在使用 primefaces fileUpload 组件,然后我检查了 FileUploadEvent.getFile 但我没有看到可靠地获取扩展名的方法。有任何想法吗? 最佳答案 getFile
在我的 GWT 项目中,我想: 为 FileUpload 小部件设置过滤器,使其只接受 JPG 文件。 启用 myButton如果 FileUpload 小部件,称为 chooser , 选择了任何文
我的 Extjs 应用程序中有一个文件上传字段。我尝试通过以下代码将文件加载到服务器: var form = Ext.getCmp('ProcImpNewSugFileForm').getForm()
长话短说,我想允许用户将文件上传到服务器。我尝试使用 Jquery 文件上传功能,但它似乎无法正常工作。 我已经简化了我的代码以查明错误/错误。我认为完成的例程没有被调用。 因此只有第一个 conso
我正在上传一个带有 PF 3.5 File Uploader 的文件 我的上传方法如下所示: public void handleFileUpload(FileUploadEvent event) {
我正在使用 FileUpload 控件上传图片,我可以使用那个浏览按钮选择图片,但是当我尝试预览所选图片时,我没有得到文件名,它显示为空.. protected void btnImgUpload_C
如何仅使用 FileUpload 代码隐藏进行上传?我的控件是代码隐藏的,因为我有 Dropdown_SelectedIndexChanged 并且需要生成各种数量的控件。我可以很好地列出控件并将文件
出于某种原因,当我尝试上传多个文件时,所有文件都成为第一个文件的副本。当我说副本时,我指的是不同的文件名、不同的文件扩展名,但所有图像中的图片相同。 例子: 我选择了四个文件。 2个png,2个jpg
我正在做一个简单的测试,只是尝试上传一个文件,将其转换为无符号 8 数组,然后将其记录到控制台 我的代码运行完美,但每次我按下按钮上传文件时,控制台都会递增地重复输出,让我解释一下: 第一次点击控制台
我正在使用 apache 的 FileUpload 将一些文件上传到我的网络服务器。问题是我不想将它们上传到机器上的特定位置,即:c:\tmp,而是上传到相对路径,例如 /ProjectName/tm
我想在我的 .FileUpload button 上应用这个 css,但它只适用于 IE 而不是 Chrome,我仍然不知道它有什么问题。有人有想法吗? 感谢帮助 .FileUpload { wid
我正在使用 asp.net C# FIleUpload。我遇到了一个问题,那就是当我上传图片并将其存储在指定的文件夹中时,刷新页面后图片会再次上传,次数与刷新页面的次数一样多。我尝试启用和禁用 Vie
我想知道为什么我的方法FileUpload.HasFile总是为null .. 这是我使用的Xaml。
我的 Web 表单上有一个 FileUpload 控件 (FileUpload1),还有一个“Sumbit”按钮、一个标签和一个包含 UserID 的隐藏字段。我在按钮的点击事件中有以下代码: str
我是一名优秀的程序员,十分优秀!