Rest 邮递员:所需请求部分';文件';不在场
我想通过postman将一个图像上传到我的RESTAPI。我使用的是spring引导框架。以下是屏幕截图: 我还没有设置任何头,正如我在其他堆栈溢出答案中发现的那样,它给出了多部分边界错误 下面是我的控制器代码:Rest 邮递员:所需请求部分';文件';不在场,rest,spring-mvc,spring-boot,multipartform-data,postman,Rest,Spring Mvc,Spring Boot,Multipartform Data,Postman,我想通过postman将一个图像上传到我的RESTAPI。我使用的是spring引导框架。以下是屏幕截图: 我还没有设置任何头,正如我在其他堆栈溢出答案中发现的那样,它给出了多部分边界错误 下面是我的控制器代码: package com.practice.rest.assignment1.controller; import java.io.IOException; import java.util.List; import org.springframework.beans.factory
package com.practice.rest.assignment1.controller;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.practice.rest.assignment1.model.Product;
import com.practice.rest.assignment1.service.CatalogueService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
@RestController
@RequestMapping("/CatalogueController/")
public class CatalogueController{
@Autowired
private CatalogueService catalogueService;
@RequestMapping(value = "addProduct", method = RequestMethod.POST , consumes = "multipart/form-data")
public Product addProduct(@RequestParam String productJson, @RequestParam MultipartFile file) throws JsonParseException, JsonMappingException, IOException {
Product product = new ObjectMapper().readValue(productJson, Product.class);
byte[] mediaBytes = file.getBytes();
product.setImage(mediaBytes);
return catalogueService.saveInDb(product);
}
}
现在,我获取一个产品对象,它在内部由一个定义为byte[]数组的图像组成。我把它作为字符串,把图像作为多部分文件
下面是我定义的产品类属性:
private Long pId;
private String model;
private String brand;
private byte[] image; // This is where I want the image to save
private Long price;
private String currency;
private String transmissionType;
private String fuelType;
由于我使用的是spring boot,下面是我的主要类:
package com.practice.rest.assignment1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我得到的邮递员错误是:
{
"timestamp": 1478611635977,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"message": "Required request part 'file' is not present",
"path": "/CatalogueController/addProduct"
}
我哪里错了?我认为问题在于您发送的JSON参数。在postman中,您不需要将“开始和结尾”作为字符串来表示参数。
此外,如果您使用“开始和结束”,那么在JSON内部(对于JSON对象,表示属性键和值),您应该使用“(单引号)。Hi@Benjamin我做了相同的演示,还有我的请求后副本:
curl 'http://localhost:9999/api/v1/upload' -H 'Pragma: no-cache' \
-H 'Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm' \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' \
-H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundary0ae4CymwYLjdqdI1' \
-H 'Accept: */*' -H 'Cache-Control: no-cache' \
-H 'Cookie: _ga=GA1.1.433565887.1474948752' \
-H 'Connection: keep-alive' -H 'DNT: 1' --data-binary $'------WebKitFormBoundary0ae4CymwYLjdqdI1\r\nContent-Disposition: form-data; name="file"; filename="228cb73.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary0ae4CymwYLjdqdI1\r\nContent-Disposition: form-data; name="a"\r\n\r\n123\r\n------WebKitFormBoundary0ae4CymwYLjdqdI1--\r\n' --compressed
我的上传控制器是这样写的:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity handleUpload(
@RequestParam("a")String a,
@RequestParam("file") MultipartFile multipartFile) throws IOException {
System.out.println(a);
...
在我的控制台中,成功输出参数a
:
即使我再次使用json字符串发送请求:
curl 'http://localhost:9999/api/v1/upload' -H 'Pragma: no-cache' -H 'Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBTudL55M6S8ENLVt' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Cookie: _ga=GA1.1.433565887.1474948752' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary $'------WebKitFormBoundaryBTudL55M6S8ENLVt\r\nContent-Disposition: form-data; name="file"; filename="228cb73.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryBTudL55M6S8ENLVt\r\nContent-Disposition: form-data; name="a"\r\n\r\n"{"key":"value"}"\r\n------WebKitFormBoundaryBTudL55M6S8ENLVt--\r\n' --compressed
看起来您的代码还可以,但您是否会尝试使用
curl
再次发送请求,可能是因为您使用了邮递员。尝试删除'Content-Type:multipart/form data…'
,标题部分。它为我解决了这个问题。对我来说,在application.properties中设置这些变量是有效的:
对于我来说,通过更新变量更新application.properties是有效的 spring.http.multipart.enabled=true 我的服务是
@PostMapping(value = "/console")
public ResponseEntity<RxConsoleDTO> addRxConsole(@RequestParam("attachment") MultipartFile attachmentFile, @RequestParam("type") String type) throws IOException {// your logic}
@PostMapping(value=“/console”)
public ResponseEntity addRxConsole(@RequestParam(“附件”)MultipartFile attachmentFile,@RequestParam(“type”)String type)抛出IOException{//your logic}
也检查下面的属性是否被启用,如果我们正在上传一个文本文件,它不会被认为是适当的参数。
upload.file.extensions=jpg, jpeg, gif, png
我刚才碰到了你的问题,因为我也遇到了同样的错误。 注意:我正在编写一个pyTest来测试我的API 我刚从文件中删除了以下几行
'Content-Type': 'multipart/form-data; boundary=--------------------------561045520875406684819091',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
而且,它起作用了:)我只需要删除开头和结尾的双引号。奇怪的是,在文件请求部分抛出了错误。谢谢/上载使用上载文件的相对路径,这可能会有所帮助:
code
@ConfigurationProperties(“存储”)公共类存储属性{/***用于存储文件的文件夹位置*/private String location=“your/absolute/path”;公共字符串getLocation(){return location;}public void setLocation(字符串位置){this.location=location;}Springboot示例中的code
:它有以下内容:spring.servlet.multipart.max file size=128KB
spring.servlet.multipart.max request size=128KB
spring.http.multipart.enabled=false
将enabled=false更改为true(根据您的回答)修复了“file”对我来说不存在的问题。谢谢!
'Content-Type': 'multipart/form-data; boundary=--------------------------561045520875406684819091',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',