邮递员:所需的请求部分“文件”不存在

新手上路,请多包涵

我想通过邮递员将图像上传到我的 Rest API。我正在使用弹簧启动框架。这是屏幕截图:

在此处输入图像描述

我也 没有设置任何标题,因为我在其他堆栈溢出答案中发现它给出了多部分边界错误。

现在,下面是我的控制器代码:

 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);

    }

}

现在,我正在使用一个 Product 对象,该对象在内部包含一个定义为 byte[] 数组的图像。我将其作为字符串和图像分别作为 Multipart 文件。

以下是我定义的产品类属性:

     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,所以这里是我的 Main 类:

 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"
}

我哪里错了?

原文由 Number945 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 628
2 个回答

我认为问题在于您发送的 JSON 参数。在邮递员中,您不需要将开头和结尾 “ 以将参数表示为字符串。而且,如果您使用开头和结尾 “,那么在 JSON(对于 JSON 对象的属性键和值的意思)中,您应该使用 ‘(单引号)。

原文由 Lipu 发布,翻译遵循 CC BY-SA 3.0 许可协议

尝试删除 'Content-Type: multipart/form-data...' ,标题部分。它为我解决了这个问题。

原文由 kenadet 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进