赞
踩
记录瞬间
近段时间使用Springboot实现了文件的上传服务,但是在使用python的requests进行post上传时,总是报错。
比如:
1、Current request is not a multipart request
2、Required request part 'fileName' is not present
3、MissingServletRequestPartException: Required request part 'fileName' is not present
4、the request was rejected because no multipart boundary was found
后来进过多次查找,终于找到一个办法,将此问题解决。
//java实现的上传接口
/*** 上传文件
*@paramfile
*@return
*/@RequestMapping(value= "/uploadFile", method = RequestMethod.POST , consumes = "multipart/form-data")
@ResponseBodypublic ResultData uploadFile(@RequestParam("fileName") MultipartFile file, @RequestParam("filePath") String zipPath) {
String msg= "AIOps, Always Online! Always in!";//判断文件是否为空
if(file.isEmpty()) {return new ResultData(false, msg, "失败了");
}
String fileName= file.getOriginalFilename(); //传入的文件名
String filePath = getOsPath(); //获取本地基本路径
String path = filePath + "/" +fileName;
System.out.println(fileName);
System.out.println(zipPath);
File dest= newFile(path);//判断文件是否已经存在
if(dest.exists()) {return new ResultData(false, msg, "失败了");
}//判断文件父目录是否存在
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdir();
}try{
file.transferTo(dest);//保存文件
} catch(IOException e) {return new ResultData(false, msg, "失败了");
}return new ResultData(true, msg, "成功了");
}/*** 获取操作系统的基本路径*/
privateString getOsPath(){
String osPath= "./";if (System.getProperty("os.name").contains("Windows")) {
osPath=config.getWindows_basedir();
}else if (System.getProperty("os.name").contains("Linux")) {
osPath=config.getLinux_basedir();
}else{
LOG.info("Unknown System..." + System.getProperty("os.name"));
}returnosPath;
}
使用python进行连接的代码如下:
from urllib3 importencode_multipart_formdataimportrequests
data={"filePath": "path/for/test"}
header={}
data['fileName'] = ("fileName", open(r"D:\path\to\file", 'rb').read())
encode_data=encode_multipart_formdata(data)
data=encode_data[0]
header['Content-Type'] = encode_data[1]
result= requests.post("http://localhost:8080/uploadFile", headers=header, data=data)
可以正常返回。
当然了,上传的过程中,也有可能存在服务器端报错的问题。
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
说明在执行的过程中,存在上传文件的大小限制,我们需要在 .properties文件中进行修改
Spring Boot的官方文档中有说明,原文如下
65.5 Handling Multipart File Uploads
Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1Mb per file and a maximum of 10Mb of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the multipart.maxFileSize property to -1.The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.
文档说明表示,每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。要更改这个默认值需要在配置文件(如application.properties)中加入两个配置
multipart.maxFileSize = 10Mb
multipart.maxRequestSize=100Mb
multipart.maxFileSize=10Mb是设置单个文件的大小, multipart.maxRequestSize=100Mb是设置单次请求的文件的总大小
如果是想要不限制文件上传的大小,那么就把两个值都设置为-1就行啦
如果有误,请查看文末提示官方文档哈
Spring Boot1.4版本后配置更改为:
spring.http.multipart.maxFileSize = 10Mb
spring.http.multipart.maxRequestSize=100Mb
Spring Boot2.0之后的版本配置修改为:
spring.servlet.multipart.max-file-size = 10MB
spring.servlet.multipart.max-request-size=100MB
=============底线=============
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。