Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从React/Node上载多部分文件时出现问题_Java_Node.js_Reactjs_Spring Mvc_Multipartform Data - Fatal编程技术网

Java 从React/Node上载多部分文件时出现问题

Java 从React/Node上载多部分文件时出现问题,java,node.js,reactjs,spring-mvc,multipartform-data,Java,Node.js,Reactjs,Spring Mvc,Multipartform Data,我有以下JAVA控制器: @RequestMapping(value = "/data/upload", method = RequestMethod.POST) public @ResponseBody void uploadData(@RequestParam("file") MultipartFile file) throws IOException { logger.info("Receiv

我有以下JAVA控制器:

    @RequestMapping(value = "/data/upload", method = RequestMethod.POST)
    public
    @ResponseBody
    void uploadData(@RequestParam("file") MultipartFile file) throws IOException {
        logger.info("Received File for Ingestion");
        dataUploadService.processData(file.getInputStream());
    }
节点服务器端代码:

serviceCall(serviceCallRequestData, request, finalResponse) {
        logger.info('Making remote request: ' + JSON.stringify(serviceCallRequestData));

        let file = request.files['file']; // file: Object {name: "sample_aspect_review_upload.csv", encoding: "7bit", mimetype: "text/csv", mv: }
        let formData = new FormData();
        formData.append('file', Buffer.from(file.data));

        fetch(serviceCallRequestData.url, {
            method: serviceCallRequestData.requestObject.method,
            headers: serviceCallRequestData.requestObject.headers,
            body: formData
        }).then(response => {
            if (response.status !== 200) {
                logger.error(`Error while making http call requestData: ${JSON.stringify(serviceCallRequestData)}`);
                finalResponse.status(500).send('Internal server error');
                return;
            }

            return response.json();
        }).then((json) => {
            logger.info(`Returning response for aspect-review-file-upload: ${JSON.stringify(json)}`);
            finalResponse.status(200).send(JSON.stringify(json));
        }).catch((e) => {
            logger.error(`Error while making http call requestData: ${JSON.stringify(serviceCallRequestData)} error: ${JSON.stringify(e)}`);
            finalResponse.status(500).send('Internal server error');
        });
    }
正在尝试上载csv文件,如:

"product_id","review_id","aspect_id","rating","confidence_score","indices"
"pid","rid","aid",1,0.851955,"[{\"s\":0,\"e\":114,\"highlights\":[39,68]}]"
邮递员很容易上传。请参见下面的屏幕截图:

在JAVA中获取错误:收到未知异常org.springframework.web.bind.MissingServletRequestParameterException:所需的多部分文件参数“file”不存在 当我不从React传递contentType标头时

在JAVA中获取错误:org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为在我从React以“内容类型”传递contentType标头时未找到多部分边界::“多部分/表单数据”

JAVA dropwizard控制器的节点服务器端代码与:

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadFile(
            @FormDataParam("file") InputStream inputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail
    ) throws IOException {
        logger.debug("Request to upload data file-name: {}", fileDetail.getName());

        dataUploadService.processData(inputStream);
        return Response.ok().build();
    } 
工作正常

我做错了什么?

一些猜测:

  • 在REST端点定义中,您将文件指定为请求参数,该参数用于从URI本身提取参数,而多部分文件上载位于POST请求的主体中

    添加
    使用={}
    而不是
    @RequestParam
    ,您可以使用
    @RequestBody
    注释:

    @RequestMapping(value = "/data/upload", method = RequestMethod.POST,
      consumes = {"multipart/mixed", "multipart/form-data"}
    )
    void uploadData(@RequestBody MultipartFile file) //body will be the whole JSON payload
    
    或者更好地使用
    @RequestPart
    ,您可以在其中命名元素:

    @RequestMapping(value = "/data/upload", method = RequestMethod.POST,
      consumes = {"multipart/mixed", "multipart/form-data"}
    )
    void uploadData(@RequestPart (value="file") MultipartFile file) //gets file from JSON
    
  • 提供用于
    FormData
    的选项:

    form.append('file', Buffer.from(file.data), {
      filename: 'image1.jpg', // ... OR:
      filepath: 'photos/image1.jpg',
      contentType: 'image/jpeg',
      knownLength: 19803
    });
    
    form.submit('http://example.com/', function(err, res) {
      if (err) throw err;
      console.log('Done');
    });
    

  • 您能否确认(使用类似于WireShark)从React发送的确切内容?

    从《邮差卷发》中,您可以看到以下命令:

    curl -i -X POST -H "Content-Type:multipart/form-data" -F "file=@\"/pathto/sample.csv\";type=text/csv;filename=\"sample.csv\"" 'http://localhost:8080/data/upload'
    
    所以我尝试添加
    type=text/csv;文件名=\“sample.csv\”
    在表单数据中 因此,这应该是可行的:

        let file = request.files['file'];
        let formData = new FormData();
        formData.append("file", Buffer.from(file.data), {
            filename: file.name,
            contentType: file.mimetype,
        });
    
        fetch(serviceCallRequestData.url, {
            method: serviceCallRequestData.requestObject.method,
            headers: serviceCallRequestData.requestObject.headers,
            body: formData
        }).then(response => {
            if (response.status !== 200) {}
        });
    

    您正在从节点服务器发送哪些标头?您是否包含了
    “内容类型”:“多部分/表单数据”
    标题?@TarunLalwani:我已经清楚地提到了使用
    “内容类型”
    标题发送数据时遇到的错误。我将当前答案视为后端所需的更改,您是否正在寻找前端代码的修复程序以使其正常工作,或者您正在评估是否也将更改后端作为一个选项?@TarunLalwani:更喜欢前端更改,因为当我从POSTMAN处点击它时,API没有问题。您能否在FE和BE之间放置一个
    socat
    ,并比较这两个请求?这并没有解决问题。在对POSTMAN的API进行了这些更改之后[@RequestPart]仍然有效,但在节点服务器上,它无法像上面提到的那样声明MultipartException。@User_Targaryen向spring的端点添加了consumes,并为Reactwild guess添加了选项,但我将重点关注节点代码中的传递“头”。原因-POST stack有时可能会在代码设置的标题之外添加标题。这些“推断”头VAL他们并不总是在帮助别人process@diginoise:使用
    @RequestBody MultipartFile file
    时,请求到达JAVA控制器,但
    file
    null
    。为什么?感谢您在
    FormData