一、配置tomcat服务器
1.1、添加upload文件夹
在webapps\Root文件夹下创建用于接收上传文件的upload文件夹
1.2、修改conf\web.xml设置允许上传文件
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
1.3、启动tomcat服务器
二、后台开发
新建web项目,在pom.xml中添加依赖
<!-- 文件上传依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- servlet依赖,用于接收多媒体文件-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!--jersey跨服务器上传依赖-->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
application.yml配置上传文件的大小
server:
port: 8070
spring:
servlet:
multipart:
#设置单个文件的大小,-1表示不限制,单位MB
max-file-size: 10MB
#设置单次请求的文件总大小,-1表示不限制,单位MB
max-request-size: 100MB
Jersey工具类
package demo.utils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
/**
* 跨服务器文件上传工具类
*
* @author qin
*
*/
public class JesyFileUploadUtil {
/**
* 上传文件
*
* @param file --文件名
* @param serverUrl --服务器路径http://127.0.0.1:8080/upload
* @return
* @throws IOException
*/
public static String uploadFile(MultipartFile file, String serverUrl) throws IOException {
String resultPath="";//文件上传后的路径
// 第一步:给文件设置个名称,保证唯一
String fileName=null;
//--获取当前时间的时间戳(毫秒数)
long timeStamp = new Date().getTime();
fileName=fileName+timeStamp;
//--随机生成三位数,拼接到时间戳后面
Random r = new Random();
for (int i=0;i<3;i++){
//--生成一个0-10之间的随机整数
fileName=fileName + r.nextInt(10);
}
//--获取原文件的后缀名
//--#-获取原版文件的文件名,比如aaa.png
String sourceFileName = file.getOriginalFilename();
//--#-获取文件的后缀名
String suffix = sourceFileName.substring(sourceFileName.lastIndexOf("."));
fileName = fileName + suffix;//时间戳+3位随机数+后缀名
//第二步:创建Jersey对象,进行文件上传
Client client = Client.create();
//--得到的效果:http://127.0.0.1:8080/upload/xxx.png
WebResource webResource = client.resource(serverUrl+"/"+fileName);
//第三步:上床文件
webResource.put(String.class, file.getBytes());
//第四步:获取文件路径
return fileName;
}
}
Controller
package demo.controller;
import demo.utils.JesyFileUploadUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
public class FileController {
@RequestMapping("/uploadFile")
@CrossOrigin(origins = "*")
public String upload(MultipartFile file){
String fileName="";
String serverUrl="http://192.168.2.51:8080/upload";
try {
fileName = JesyFileUploadUtil.uploadFile(file, serverUrl);
} catch (IOException e) {
e.printStackTrace();
}
return serverUrl+"/"+fileName;
}
}
三、前端开发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="assets/vue.min-v2.5.16.js"></script>
<script src="assets/axios.min.js"></script>
</head>
<body>
<div id="app">
<input type="file" @change="upload" />
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
imgURL:null
},
methods: {
//上传文件
upload(event){
const file = event.target.files[0];//获取要上传的文件
// 在这里进行一系列的校验
const formData = new FormData();
formData.append("file",file);//传参,参数名:file
//通过axios发送请求
axios.post('http://localhost:8070/uploadFile',formData,{
'Content-type' : 'multipart/form-data'
}).then(response=>{
console.log(response.data);
this.imgURL=response.data;
},err=>{
console.log(err)
})
}
}
});
</script>
</html>