- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章利用MultipartFile实现文件上传功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的MultipartFile实现文件上传,感觉挺简单,在这里和大家分享一下.
一.主要有两个java类,和一般的servlet放在一起即可. 。
1.FileUploadBean.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
chb.demo.web;
import
org.springframework.web.multipart.MultipartFile;
/**
* @author chb
*
*/
public
class
FileUploadBean {
private
MultipartFile file;
public
void
setFile(MultipartFile file) {
this
.file = file;
}
public
MultipartFile getFile() {
return
file;
}
}
|
2.FileUploadController.java 。
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
|
package
chb.demo.web;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.springframework.validation.BindException;
import
org.springframework.web.multipart.MultipartFile;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.SimpleFormController;
/**
* @author chb
*
*/
public
class
FileUploadController
extends
SimpleFormController {
protected
ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors){
try
{
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
// let's see if there's content there
MultipartFile file = bean.getFile();
if
(file ==
null
) {
throw
new
Exception(
"上传失败:文件为�空"
);
}
if
(file.getSize()>
10000000
)
{
throw
new
Exception(
"上传失败:文件大小不能超过10M"
);
}
//得到文件�名
String filename=file.getOriginalFilename();
if
(file.getSize()>
0
){
try
{
SaveFileFromInputStream(file.getInputStream(),
"D:/"
,filename);
}
catch
(IOException e) {
System.out.println(e.getMessage());
return
null
;
}
}
else
{
throw
new
Exception(
"上传失败:上传文件不能为�空"
);
}
// well, let's do nothing with the bean for now and return:
try
{
return
super
.onSubmit(request, response, command, errors);
}
catch
(Exception e) {
System.out.println(e.getMessage());
return
null
;
}
}
catch
(Exception ex)
{
System.out.println(ex.getMessage());
return
null
;
}
}
/**保存文件
* @param stream
* @param path
* @param filename
* @throws IOException
*/
public
void
SaveFileFromInputStream(InputStream stream,String path,String filename)
throws
IOException
{
FileOutputStream fs=
new
FileOutputStream( path +
"/"
+ filename);
byte
[] buffer =
new
byte
[
1024
*
1024
];
int
bytesum =
0
;
int
byteread =
0
;
while
((byteread=stream.read(buffer))!=-
1
)
{
bytesum+=byteread;
fs.write(buffer,
0
,byteread);
fs.flush();
}
fs.close();
stream.close();
}
}
|
二.配置文件中如下配置
1.web.xml,利用spring mvc模式,大家应该都很熟悉了 。
1
2
3
4
5
6
7
8
9
10
|
<
servlet
>
<
servlet-name
>chb</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>chb</
servlet-name
>
<
url-pattern
>*.do</
url-pattern
>
</
servlet-mapping
>
|
2.chb-servlet.xml,这里要配置映射,并可以设定最大可上传文件的大小 。
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<
beans
>
<!-- Multi-Action 用来标识method的变量名定义-->
<
bean
id
=
"methodNameResolver"
class
=
"org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"
>
<
property
name
=
"paramName"
>
<
value
>action</
value
>
</
property
>
<
property
name
=
"defaultMethodName"
>
<
value
>index</
value
>
</
property
>
</
bean
>
<
bean
id
=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
<!-- one of the properties available; the maximum file size in bytes -->
<
property
name
=
"maxUploadSize"
value
=
"10000000"
/>
</
bean
>
<
bean
id
=
"handlerMapping"
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<
property
name
=
"mappings"
>
<
props
>
<
prop
key
=
"/upload.do"
>fileUploadController</
prop
>
</
props
>
</
property
>
</
bean
>
<
bean
id
=
"fileUploadController"
class
=
"chb.demo.web.FileUploadController"
>
<
property
name
=
"commandClass"
value
=
"chb.demo.web.FileUploadBean"
/>
<!-- 上传失败时跳转页面 -->
<
property
name
=
"formView"
value
=
"/user/err.jsp"
/>
<!-- 上传成功时跳转页面 -->
<
property
name
=
"successView"
value
=
"/user/confirmation.jsp"
/>
</
bean
>
</
beans
>
|
三.设定jsp页面 。
1
2
3
4
5
6
7
8
9
|
<
form
id
=
"form1"
method
=
"post"
action
=
"upload.do"
enctype
=
"multipart/form-data"
>
<
tr
>
<
td
width
=
"25%"
align
=
"right"
>上传文件:</
td
>
<
td
><
input
id
=
"file"
type
=
"file"
NAME
=
"file"
style
=
"width:300px;"
></
td
>
</
tr
>
<
tr
align
=
"center"
valign
=
"middle"
>
<
td
height
=
"60"
colspan
=
"2"
><
input
type
=
"submit"
ID
=
"BtnOK"
value
=
"确认上传"
></
td
>
</
tr
>
</
form
>
|
ok,现在就可以上传文件了,挺简单吧?这里我只列出了基本步骤,至于具体的操作(比如中文问题)可能就需要大家自己再完善完善了. 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/hbcui1984/article/details/1498112 。
最后此篇关于利用MultipartFile实现文件上传功能的文章就讲到这里了,如果你想了解更多关于利用MultipartFile实现文件上传功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用 Spring 4 和 Java Config。我想将多个文件上传到服务器,但问题是我的 MultipartFile[ ] 参数将始终接收空/[ ] 参数。让我在这里分享我的代码是我的“Ap
我正在使用 MultPartFile 从前端上传 .zip,有时在我对其执行任何操作之前,该文件会被删除。我调试 CommonsMultipartFile.class (intellij) 来清理我的
我面临以下问题:我使用 MultipartFile 提交带有电子邮件等其他信息的图像。 考虑这样一种情况,我们有两个字段:输入 = 带电子邮件的文本和带文件的输入(输入类型 = 文件)。在服务器端,我
我需要将从客户端接收到的 MultipartFile 转换为字节数组,以将其发送到另一台服务器(无需在第一台服务器上保存文件)。我想使用 MultipartFile.getBytes() 函数,将其转
有什么方法可以将 File 对象转换为 MultiPartFile?这样我就可以将该对象发送到接受 MultiPartFile 接口(interface)对象的方法? File myFile = ne
我有一个使用 spring boot 的 Java 11 应用程序。 我的请求 Controller 中有这个请求: @RestController public class ImportContro
我以 BASE64 编码字符串(encodedBytes)的形式接收图像,并使用以下方法在服务器端解码为 byte[]。 BASE64Decoder decoder = new BASE64Decod
我希望用户能够提交任意数量的具有任意名称和原始文件名的文件。 如何获取请求中已提交文件的列表? @RequestParam(name = "*") MultipartFile[] files 我希望使
我尝试通过 post 方法将图像从ionic前端应用程序发送到中的后端服务 Spring 启动 我已经完成了这个方法,使用 FormData 对象内的图像将帖子发送到后端 url: uploadI
如果存在 MultipartFile,为什么 @Valid 注释不适用于我的域模型?我的 Controller 中有以下代码: @PostMapping("/create") public Strin
我有一个可以上传文件的表单。有一个问题,即使没有选择或上传文件,@RequestParam 值的大小也是我希望它为空或 null 的大小。 最佳答案 根据this official这里的 Mozill
我正在使用 MultipartFile 发送包含多个附件的电子邮件。我的代码工作正常,但我将每个文件存储在我的项目中,然后附加。我不想将该文件存储在任何地方,而是希望将文件直接发送给收件人。我的代码是
我正在尝试获取MultipartFile的内容,它是通过MultipartHttpServletRequest.getFile()获取的。 MultipartFile 中有两个函数: bytes[]
我想提取文件路径然后可以上传到谷歌驱动器。如何从 MultipartFile 获取路径? //logger.info(""+uploadFile.getClass().getResource(
我通过文件转换为MultipartFile,但是MultipartFile是空的,请帮忙确认一下,非常感谢,代码如下: File file = new File("/Users/aikaliu/doc
我正在尝试上传超过 1 GB 的文件,我正在使用 Spring Boot。 我已尝试使用以下代码,但出现内存不足错误。 public void uploadFile(MultipartFile fil
我正在使用此代码将图像文件发布到我的 Controller ,但我总是得到文件正文部分的空值。 @RequestMapping(value = "/updateprofile", method = R
我正在使用 angularJs 和 spring 4.0, 我的 Controller 代码: @RequestMapping(value = "/endpoint/add", method = Re
以下是我从网页上传多个文件的函数。我的文件位于 MultipartFile --> 文件变量列表中。我无法迭代每个文件。然而 log.info("file"+files) 打印了 file[]。这意味
我有这样的用户界面代码 .factory('Service', function ($http, $resource, BASE_PATH) { function sendResponse (co
我是一名优秀的程序员,十分优秀!