【问题标题】:angular multipart post request to jersey rest api fails with http 400 invalid syntax对 jersey rest api 的角度多部分发布请求失败,http 400 语法无效
【发布时间】:2021-05-20 15:41:47
【问题描述】:

我使用 Angular 9 将 POST 请求发送到在 Tomcat 9 和 Jersey 2.32 上运行的 REST API。

我的所有请求都运行良好,除了我当前正在开发的请求,它是一个上传文件然后在该文件上运行任务的 POST 请求。

虽然文件已上传且进度指示器正确,但我在此请求上收到 HTTP 400 错误,请求语法无效。

我无法弄清楚我的代码有什么问题,因此欢迎提出任何建议。

这是我发送 POST 请求的 Angular 代码:

   const authorizationString = GlobalVariablesService.customerName + ':' + this.token.getToken();
   const requestOptions = {
     reportProgress: true,
     headers: new HttpHeaders({
       'Content-Type': 'multipart/form-data',
       Authorization: authorizationString
     }),
   };
   const formData = new FormData();
   formData.append('file', GlobalVariablesService.selectedFile);
   const req = new HttpRequest(
     'POST',
     GlobalVariablesService.adminConfig.metaLmsApiUrl + 'userImport',
     formData,
     requestOptions
   );
   return this.httpClient.request(req).pipe(
     map(event => this.getEventMessage(event, GlobalVariablesService.selectedFile)),
     tap(message => this.showProgress(message)),
     last(), // return last (completed) message to caller
     catchError(this.handleError(GlobalVariablesService.selectedFile)),
   );

这是接收请求的Java代码:

@Path("/userImport")
public class UserImportRest {

   ErrorResponseRec errorResponse;

   @POST
   @Consumes(MediaType.MULTIPART_FORM_DATA)
   @Produces({ MediaType.APPLICATION_JSON })   
   public ErrorResponseRec uploadFile(
           @FormDataParam("file") InputStream uploadedInputStream,
           @FormDataParam("file") FormDataContentDisposition fileDetail) {
       // save file
       System.out.println("upload here");
       String uploadedFileLocation = "C://LMS//resources//local//uploaded/" + fileDetail.getFileName();
       writeToFile(uploadedInputStream, uploadedFileLocation);
       String output = "File uploaded to : " + uploadedFileLocation;
       errorResponse = new ErrorResponseRec(output);
       return errorResponse;
   }

   // save uploaded file to new location
   private void writeToFile(
           InputStream uploadedInputStream,
           String uploadedFileLocation) {

       try {
           OutputStream out
                   = new FileOutputStream(
                           new File(
                                   uploadedFileLocation));
           int read = 0;
           byte[] bytes = new byte[1024];

           out = new FileOutputStream(new File(uploadedFileLocation));
           while ((read = uploadedInputStream.read(bytes)) != -1) {
               out.write(bytes, 0, read);
           }
           out.flush();
           out.close();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }

}

【问题讨论】:

    标签: angular jersey multipart


    【解决方案1】:

    感谢FormData how to get or set boundary in multipart/form-data - Angular,这是修复:

    不要设置 Content-Type 标头。

        const requestOptions = {
          reportProgress: true,
          headers: new HttpHeaders({
            Authorization: authorizationString
          }),
        };
    

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2012-12-31
      相关资源
      最近更新 更多