springboor ant design vue上传大文件失败 ,或者一般大小的也会失败
前端
/**
* 上传 axios
*/
export function getBackupUpload(parameter) {
console.log(parameter)
return axios({
url: '/main/selectBackup/upload',
method: 'post',
headers: {
'content-Type': 'multipart/form-data'
},
data: parameter,
})
}
// 自定义上传,不用通过action属性
customRequest(file) {
this.uploadLoading=true
console.log(file)
const form = new FormData()
form.append('file', file.file)
form.append('contractName', file.file.name)
form.append('description', file.file.name)
//自定义上传接口
getBackupUpload(form).then(res => {
console.log(res)
this.$message.success('操作成功,文件恢复中,请耐心等待')
this.uploadLoading=false
}).catch(err => {
console.log(err)
this.$message.error('文件不符合规范,已删除')
this.uploadLoading=false
})
},
后端
@PostMapping(value = "/upload")
public ResponseData uploadFile(@RequestParam("file") MultipartFile file) throws IOException, InterruptedException {
if (file.isEmpty()) {
// return "未选择文件";
}
String fileName = file.getOriginalFilename();
System.out.println("接收上传文件");
System.out.println(str);
String path = "C:\\Users\\x\\Desktop\\";
System.out.println(path+""+str);
file.transferTo(new File(path+fileName));
return new SuccessResponseData(200, "执行成功", "");
}
这个一般是服务端设置了最大文件限制,找下看springboot本身的配置文件,或者你使用的web容器的配置文件。tomcat、apache、nginx等的限制。
希望可以帮到你
具体包啥错?
试一下在配置文件(application.properties或者application.yml),加上这几个参数(允许上传文件的大小)
server.tomcat.max-http-form-post-size = 30MB
spring.servlet.multipart.maxFileSize = 30MB
spring.servlet.multipart.maxRequestSize = 100MB
第一步:修改springboot multipart的配置
spring:
servlet:
multipart:
max-file-size: 5000MB
max-request-size: 5000MB
第二步:修改tomcat服务器连接时间(还是springboot的配置文件)
server:
connection-timeout: 18000000
前端依旧报错:报错如下
第三步:增大前端VUE 发送异步请求axios的超时时间
customRequest(data){ // 上传提交
this.$message.success("文件正在上传!");
const formData = new FormData() ;
formData.append('file', data.file);
console.log("data"+data);
formData.append('token', "token")
this.axios({
method: 'post',
timeout: 900000, //这个就是重点
url: '/hdfs/saveFile',
headers: {
},
params:{
},
data: formData
}).then((response) => {
console.log(response)
data.onSuccess();
}).catch(function (error) {
data.onError();
console.log(error)
})
},
直接在后台 配置文件可以配的