java实现多文件上传至本地服务器功能
作者:程序员阿坤 时间:2022-01-26 11:55:24
博主最近在做一个内网项目,内部可以访问外部数据,但是外部访问不了内部数据,这也就造成了可能文件无法上传,所以博主另辟蹊径,在本地服务器上建立一个文件夹专门用来存储上传数据。
环境:jdk,tomcat
一、前台上传文件(ajax上传)
<input type="file" name="annexUrl" id="annexUrl" multiple="multiple"/>
其中multiple=“multiple”,设置文件多上传
function uploadFile() {
var files = document.getElementById("annexUrl").files;
if (files.length != 0) {
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
formData.append(file.name, file);
}
$.ajax({
url: 'cdc/public/saveFiles',
type: 'POST',
cache: false,
data: formData,
//这个参数是jquery特有的,不进行序列化,因为我们不是json格式的字符串,而是要传文件
processData: false,
//注意这里一定要设置contentType:false,不然会默认为传的是字符串,这样文件就传不过去了
contentType: false,
success: function (data) {
save(data.data);
}
});
} else {
save();
}
}
这里需要使用到formData对象用来封装file文件对象,save()方法是用来保存上传之后返回过来的文件路径,保存到数据库,便于下载。
二、后台接收文件,并上传至服务器
// 多文件上传,返回一个对象集合(附件地址,名称)
@RequestMapping(value = "saveFiles", method = RequestMethod.POST)
@ResponseBody
public JSONObject saveFiles(HttpServletRequest request, HttpServletResponse response) {
JSONObject jsonObject = new JSONObject();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Collection<Part> parts = null;
try {
parts = request.getParts();
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
Iterator<Part> iterator = parts.iterator();
// 名称集合,返回给前台
List<String> list = new ArrayList<>();
while (iterator.hasNext()) {
Part part = iterator.next();
// 生成实际存储的真实文件名(唯一)
// 不知道为什么,文件上传必须得包含获取到的文件名,否则报文件名、目录名或卷标语法不正确,图片上传没有这种限制
// 文件名,保存导数据库,用来界面显示
String name = part.getName();
String realName = UUID.randomUUID().toString() + name;
list.add(name + "&&" + realName);
///home/tomcat/apache-tomcat-9.0.1/files
String realPath = "D:" + File.separator + "apache-tomcat-8.5.15" + File.separator + "files";
// String realPath = "C:" + File.separator + "XHJ224" + File.separator + "software" + File.separator + "apache-tomcat-9.0.1" + File.separator + "files";
// String realPath = File.separator + "home" + File.separator + "tomcat" + File.separator + "apache-tomcat-9.0.1" + File.separator + "files";
File file = new File(realPath);
//如果目录不存在
if (!file.isDirectory()) {
//创建文件上传目录
file.mkdirs();
}
// 文件存放的真实路径
String filePath = realPath + File.separator + realName;
try {
part.write(filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
jsonObject.put("data", list);
return jsonObject;
}
其中list集合添加了一个由文件名和真实文件名拼接的字符串,后期save需要。realName是为了防止多上传文件名冲突,realPath是文件保存路径,不同的操作系统有不同的路径,realPath最好放在tomcat下,便于项目移植。
其中File.separator为路径分隔符,他能自动识别是哪个操作系统而使用不同的路径分隔符(windows是‘\',linux是‘/')。最后将list返回给前台。
本文已被整理到了《Java上传操作技巧汇总》,欢迎大家学习阅读。
来源:http://blog.csdn.net/KKKun_Joe/article/details/78338552
标签:java,文件上传,服务器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot自定义Starter的具体流程
2022-01-26 05:08:06
一键清除maven仓库中下载失败的jar包的实现方法
2023-08-13 03:43:59
![](https://img.aspxhome.com/file/2023/4/58174_0s.png)
Java面试synchronized偏向锁后hashcode存址
2023-08-09 09:15:06
![](https://img.aspxhome.com/file/2023/0/57920_0s.png)
利用Kotlin + Spring Boot实现后端开发
2022-02-20 05:30:19
![](https://img.aspxhome.com/file/2023/1/69281_0s.jpg)
Java对zip,rar,7z文件带密码解压实例详解
2023-11-29 05:08:32
![](https://img.aspxhome.com/file/2023/4/60674_0s.png)
IntellJ IDEA神器使用技巧(小结)
2023-08-08 22:01:44
java生成图片验证码实例代码
2022-01-06 04:01:37
Java 实现分布式服务的调用链跟踪
2023-11-25 10:24:55
![](https://img.aspxhome.com/file/2023/4/60124_0s.png)
Java 根据网址查询DNS/IP地址的方法
2023-06-21 15:31:54
Maven打包没有指定主类问题(xxx.jar中没有主清单属性)
2023-11-26 21:34:44
javaweb中Filter(过滤器)的常见应用
2023-07-10 23:57:36
java进阶解析Springboot上传excel存入数据库步骤
2022-01-07 15:18:51
![](https://img.aspxhome.com/file/2023/4/62694_0s.png)
JAVA基于数组实现的商品信息查询功能示例
2021-10-31 11:47:23
java中初始化MediaRecorder的实现方法
2023-11-29 03:54:52
Java序列化和反序列化示例介绍
2023-11-25 04:24:26
Java Web程序实现返回JSON字符串的方法总结
2023-07-28 22:46:48
![](https://img.aspxhome.com/file/2023/9/66169_0s.png)
IDEA+JRebel实现全自动热部署的方法步骤
2023-11-29 13:53:30
![](https://img.aspxhome.com/file/2023/4/60454_0s.jpg)
Java中synchronized锁的深入理解
2023-08-18 01:36:55
![](https://img.aspxhome.com/file/2023/1/64771_0s.webp)
mybatis如何实现saveOrUpdate
2021-07-18 10:33:11
IDEA + Maven环境下的SSM框架整合及搭建过程
2023-08-26 02:06:55
![](https://img.aspxhome.com/file/2023/3/58423_0s.jpg)