记录瞬间

近段时间使用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实现的上传接口

    /**
* 上传文件
* @param file
* @return
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST , consumes = "multipart/form-data")
@ResponseBody
public 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 = new File(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, "成功了");
}
/**
* 获取操作系统的基本路径
*/
private String 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"));
}
return osPath;
}

使用python进行连接的代码如下:

from urllib3 import encode_multipart_formdata
import requests 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

=============底线=============

Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法的更多相关文章

  1. 用Python的requests库作接口测试——上传文件

    POST一个多部分编码(Multipart-Encoded)的文件 Requests使得上传多部分编码文件变得很简单: >>> url = 'http://httpbin.org/p ...

  2. 接口自动化-python unittest+requests+HTMLrunner

    从2015年毕业入行软件测试,快满4年了,之前技术分享都在百度贴吧上面,现在正式开始在博客中记录工作技术,努力成长,加油 接口测试的步骤1.组装好该接口需要的参数数据2.使用get或post附带参数数 ...

  3. python接口自动化测试 - requests库的post请求进行文件上传

    前言 如果需要发送文件到服务器,比如上传图片.视频等,就需要发送二进制数据. 一般上传文件使用的都是 Content-Type: multipart/form-data; 数据类型,可以发送文件,也可 ...

  4. Python接口自动化——文件上传/下载接口

    〇.前言 文件上传/下载接口与普通接口类似,但是有细微的区别. 如果需要发送文件到服务器,例如:上传文档.图片.视频等,就需要发送二进制数据,上传文件一般使用的都是 Content-Type: mul ...

  5. python中使用multipart/form-data请求上传文件

    最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...

  6. uedit修改文件上传路劲,支持api文件接口

    首先修改一个东西ueditor/ueditor.config.js serverUrl: URL + "php/controller.php" 原来 serverUrl: &quo ...

  7. PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现

    PHP 上传文件接口: //保存图片 $json_result ['status'] = 0; $path = 'upfile'; $json_result ['status'] = 0; $json ...

  8. jmert中如何测试上传文件接口(测试上传excel文件)

    第一次用jmeter这个工具测试上传接口,以前没做过这一块,导致走了很多弯路.特地把经验谢谢,怕自己以后忘记... 一,jmeter如何上传文件 jmeter 的 http requests post ...

  9. Jmeter工具之上传图片,上传音频文件接口

    https://www.jianshu.com/p/f23f7fe20bf3 互联网时代的来临,不同手机上安装的APP,还是PC端的应用软件或多或多都会涉及到图片的上传,那么在Jmeter工具如何模拟 ...

随机推荐

  1. 服务不支持 chkconfig 的解决办法

    在chkconfig --add servername的时候老是提示服务不支持 chkconfig 经过查找,解决办法如下. 1.脚本tomcatstart前三行如下: #!/bin/bash #ch ...

  2. weblogic漏洞总结 复现(未完)

    复现方式 Docker复现 WEBlogic爆出了很多漏洞 先了解一下现在主流的版本 Weblogic 10.3.6.0 Weblogic 12.1.3.0 Weblogic 12.2.1.1 Web ...

  3. c源码编译

    #include<stdio.h> #include<math.h> //程序中要调用求平方根函数sqrt int main() { double a,b,c,disc,x1, ...

  4. centos6.5和centos7如何搭建php环境(包括php7)

    查看下centos的版本信息: #适用于所有的linux lsb_release -a #或者 cat /etc/redhat-release #又或者 rpm -q centos-release 安 ...

  5. 6 Java Shell排序

    希尔排序是先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序. 1.基本思想 将待排序数组按照步长gap进行分组,然后将 ...

  6. CMD命令行管道命令

    一.什么是管道命令 管道命令能够将一个命令的执行结果经过筛选,只保留我们需要的信息. 如 dir 命令会显示目录下所有文件夹和文件,可以使用管道命令| findstr "" 将di ...

  7. ambari部署Hadoop集群(2)

    准备本地 repository 1. 下载下面的包 wget http://public-repo-1.hortonworks.com/ambari/centos7/2.x/updates/2.7.3 ...

  8. 在oracle中使用基表建立月表的存储过程

    某些系统需要按月分表来保存数据.下面的存储过程演示了如何使用基表来建立每个月的月表. 处理思路是:     1:首先,为基表建立好表和对应的索引.     2:将基表保存到一个存储过程需要的表中.   ...

  9. tfserving 调用deepfm 并预测 java 【参考】

    https://blog.csdn.net/luoyexuge/article/details/79941565?utm_source=blogxgwz8 首先是libsvm格式数据生成java代码, ...

  10. 代码实现:从键盘输入接收一个文件夹路径,打印出该文件夹下所有的.java文件名

    package com.loaderman.test; import java.io.File; import java.io.FileReader; import java.util.Scanner ...