Java org.springframework.web.multipart.support.MissingServletRequestPartException:必需的请求部分';文件';不在场

Java org.springframework.web.multipart.support.MissingServletRequestPartException:必需的请求部分';文件';不在场,java,spring,multipartform-data,Java,Spring,Multipartform Data,我正在尝试访问另一台服务器,以便使用Java上载文件。我可以连接到服务器,但无法上载文件。 服务器(Spring Boot应用程序)以这种方式接受文件 @PostMapping(value = ("multipart-store"), headers = ("content-type=multipart/*")) CustomResponse gridFs(@RequestPart("file") MultipartFile multipartFile) throws Exception {

我正在尝试访问另一台服务器,以便使用Java上载文件。我可以连接到服务器,但无法上载文件。
服务器(Spring Boot应用程序)以这种方式接受文件

@PostMapping(value = ("multipart-store"), headers = ("content-type=multipart/*"))
CustomResponse gridFs(@RequestPart("file") MultipartFile multipartFile) throws Exception {
    return new CustomResponse(storageService.storeObject(multipartFile));
}
我尝试了几种访问服务器的方法。
下面是我访问服务器的Java代码。
第一路

在上面的addFilePart()方法中,通过替换
this.writer.append(“file:+uploadFile”)通过下面几行代码,我能够解决这个问题

this.writer.append("--" + this.boundary).append(LINE_FEED);
this.writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
this.writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
this.writer.append(LINE_FEED);

您注释掉了所有编写部分头的代码,那么为什么您认为这仍然会生成一个有效的多部分请求呢?我已经注释了“addHeaderField()”的代码,但没有注释“addFilePart()”的代码。我只是想知道用addFilePart()编写的代码是否正确。我刚才说的是
addFilePart()
中的代码,即您现在删除的注释代码。如果您从未写入开始边界线和关联的部分标题,则内容中将不会包含任何部分,这就是服务器抱怨缺少
文件
部分的原因。您没有创建有效的
multipart/*
内容。因此,不,在
addFilePart()
中编写的代码不正确。谢谢您的建议,现在我已经解决了这个问题。
public class MultipartUtility {

  private final String boundary;
  private static final String LINE_FEED = "\r\n";
  private final HttpURLConnection httpConn;
  private final String charset;
  private final OutputStream outputStream;
  private final PrintWriter writer;

  public MultipartUtility(final String requestURL, final String charset) throws IOException {
    this.charset = charset;

    this.boundary = "" + System.currentTimeMillis() + "";

    final URL url = new URL(requestURL);
    this.httpConn = (HttpURLConnection) url.openConnection();
    this.httpConn.setUseCaches(false);
    this.httpConn.setDoOutput(true); // indicates POST method
    this.httpConn.setDoInput(true);
    this.httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + this.boundary);
    this.outputStream = this.httpConn.getOutputStream();
    this.writer = new PrintWriter(new OutputStreamWriter(this.outputStream, charset), true);
  }

  public void addFilePart(final String fieldName, final File uploadFile) throws IOException {
    final String fileName = uploadFile.getName();

    this.writer.append("file : "+uploadFile);
    this.writer.flush();

    final FileInputStream inputStream = new FileInputStream(uploadFile);
    final byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
      this.outputStream.write(buffer, 0, bytesRead);
    }
    this.outputStream.flush();
    inputStream.close();

    this.writer.append(LINE_FEED);
    this.writer.flush();
  }

  public List<String> finish() throws IOException {
    final List<String> response = new ArrayList<String>();

    this.writer.append(LINE_FEED).flush();
    this.writer.append("--" + this.boundary + "--").append(LINE_FEED);
    this.writer.close();

    final int status = this.httpConn.getResponseCode();
    if (status == HttpURLConnection.HTTP_OK) {
      final BufferedReader reader = new BufferedReader(new InputStreamReader(this.httpConn.getInputStream()));
      String line = null;
      while ((line = reader.readLine()) != null) {
        response.add(line);
      }
      reader.close();
      this.httpConn.disconnect();
    } else {
      throw new IOException("Server returned non-OK status: " + status);
    }

    return response;
  }
}
final File file = new File("/home/thrymr/Desktop/invoicesample.pdf");  
final HttpPost post = new HttpPost("http://localhost:8082/data/multipart-store");  
final FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA);  
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();  

builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);  
builder.addPart("file", fileBody);  

final HttpEntity entity = builder.build();  
post.setHeader("Content-Type", "multipart/form-data; boundary=gv");  
final CloseableHttpClient client = HttpClients.createDefault();  
post.setEntity(entity);  

final HttpResponse response = client.execute(post);  
System.out.println(response);  
this.writer.append("--" + this.boundary).append(LINE_FEED);
this.writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
this.writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
this.writer.append(LINE_FEED);