SpringMvc代码
jar包
commons-fileupload
commons-io
spring-mvc.xml配置
?
|
1
2
3
|
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
</bean>
|
Controller
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@RequestMapping(value = "api/v1/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map upload (@RequestParam(value = "files") MultipartFile [] files,
@RequestParam(value = "id") String id,
HttpServletRequest request, HttpServletResponse response) {
Map res = new HashMap();
try {
log.info("upload>>>>>id:{}", id);
if (files!=null) {
for (MultipartFile file:files) {
log.info("filename:{}", file.getOriginalFilename());
}
}
} catch (Exception e) {
log.error("upload>>>>异常:{}", e.toString());
}
log.info("upload>>>>返回结果:{}", res);
return res;
}
|
保存到本地
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// copy File
public boolean copyFile (MultipartFile tempFile, String filePath) {
Boolean res = false;
try {
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 将文件拷贝到当前目录下
tempFile.transferTo(file);
res = true;
} catch (Exception e) {
log.info("copyFile>>>>异常:{}", e.toString());
}
return res;
}
|
AngularJs代码
?
|
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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="uploadCtrl">
<p><input type="file" multiple="multiple" name="files"></p>
<p><input type="text" name="id" ng-model="id"></p>
<p><input type="button" value="提交" ng-click="submit()"></p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('uploadCtrl', ["$scope", "$http", function($scope, $http) {
$scope.submit = function () {
var fd = new FormData();
var files = document.querySelector('input[name="files"]').files;
for (var i=0; i<files.length; i++) {
fd.append("files", files[i]);
}
fd.append("id", $scope.id);
$http({
method:'POST',
url : '/Project/api/v1/upload',
data: fd,
headers: {'Content-Type':undefined},
transformRequest: angular.identity
}).success(function (response) {
console.log(response.data);
}).error(function () {
});
}
}]);
</script>
</body>
</html>
|
Form表单提交
?
|
1
2
3
4
5
|
<form action="/Project/api/v1/upload" method="POST" enctype="multipart/form-data">
<p><input type="text" name="id" /></p>
<p><input type="file" multiple="multiple" id="files" name="files" /></p>
<p><input type="submit" value="Submit" /></p>
</form>
|
以上所述是小编给大家介绍的SpringMvc+Angularjs 实现多文件批量上,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/u013836363/article/details/65437141
相关文章
猜你喜欢
- 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交流群
您的支持,是我们最大的动力!
热门文章
-
spring boot 注入 property的三种方式(推荐)
2025-05-29 63 -
2025-06-04 106
-
2025-06-04 21
-
2025-05-25 84
-
2025-05-29 104
热门评论

