目的是实现异步上传
1.添加pom依赖
添加pom依赖,因为用的ajax,数据需要转成json的格式进行传输,所以还有加入一个JSON jar包:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.37</version>
</dependency>
|
2.修改配置文件
applicationContext.xml里面需要加上:
?
|
1
2
3
4
5
|
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="5400000"></property>
</bean>
|
3.前端页面上
前端页面:
?
|
1
2
3
4
5
6
7
8
|
<form id="uploadForm" name="uploadForm"
enctype="multipart/form-data">
<input name="messageContent" value="多个参数的情况下">
<label>文件</label> <input type="file" name="file">
<button class="btn" type="button" id="doSave">提交</button>
</form>
</body>
</html>
|
需要加入的JS:
?
|
1
2
3
|
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
|
JS方法:
?
|
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
|
<script>
$(function() {
$("#doSave")
.click(
function() {
$("#uploadForm")
.ajaxSubmit(
{
type : 'post',
url : "/tmpInfo/method2.do",
//data: //注意只要是写在表单里面的,都不需要加这个属性。在controller中可以根据@RequestParam String str获取到属性值。
contentType : "application/x-www-form-urlencoded; charset=utf-8",
success: function(data) {
//接受到的data还只是一个字符串,需要转成json对象
var obj = JSON.parse(data);
if(obj.flag==true){
alert("上传成功");
}else{
alert("error");
}
},
error: function (data)//服务器响应失败处理函数
{
alert("出错");
}
});
});
});
|
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
|
@RequestMapping("/method2")
@ResponseBody
public String method2(@RequestParam MultipartFile file,
@RequestParam String messageContent ) {
//多个参数的话只要多个@RequestParam即可,注意参数名要和表单里面的属性名一致
JSONObject json =new JSONObject();
System.out.println(messageContent);
String orgiginalFileName = "";
int m =new Random().nextInt(100)+10;
System.out.println("m="+m);
String path="D:/"+m+"b.txt";
try {
File newFile =new File(path);
file.transferTo(newFile);
String fileName = file.getName();
InputStream inputStream = file.getInputStream();
String content = file.getContentType();
orgiginalFileName = file.getOriginalFilename();
System.out.println("fileName: "+fileName+", inputStream: "+ inputStream
+"\\r\\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName
+"\\r\\n projectName: ");
} catch (IOException e) {
e.printStackTrace();
}
json.put("flag", true);
json.put("message", "success");
System.out.println(json.toJSONString());
return json.toJSONString();
}
|
以上就是分享给大家的关于SpringMVC使用MultipartFile 实现异步上传方法介绍的全部内容,希望对大家有所帮助。欢迎大家浏览本站其他专题,有什么疑问或者建议可以随时留言,小编会及时回复大家的。希望大家对快网idc网站多多支持!
原文链接:http://blog.csdn.net/foolishandstupid/article/details/52047514
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-05 105
-
2025-06-04 61
-
Java中HashTable和HashMap的区别_动力节点Java学院整理
2025-05-29 59 -
2025-06-04 73
-
Adobe Reader因不再吸引Linux用户决定退出Linux系统
2025-05-27 13
热门评论

