日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

springboot调用python脚本_Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法...

發布時間:2023/12/15 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot调用python脚本_Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

記錄瞬間

近段時間使用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

=============底線=============

總結

以上是生活随笔為你收集整理的springboot调用python脚本_Springboot实现上传文件接口,使用python的requests进行组装报文上传文件的方法...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。