英文:
Parsing File upload in Spring
问题
我想解析接受多部分/form-data文件的请求体。目前,当我尝试将inputStream打印为字符串时,我得到如下内容:
----------------------------559363225496099939024171
Content-Disposition: form-data; 
name="file"; 
filename="upload.txt"
Content-Type: text/plain
{  
    "documents": [    
        {      
            "id": "1",
            "text": "abc"    
        }  
     ]
}
----------------------------559363225496099939024171
以下是我的controller.java:
@POST
@Path("/modelInfo")
@Produces({ "application/json" })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response getModel(InputStream file) throws IOException {
    InputStreamReader isReader = new InputStreamReader(file, StandardCharsets.UTF_8);
    //Creating a BufferedReader object
    BufferedReader reader = new BufferedReader(isReader);
    StringBuffer sb = new StringBuffer();
    String str;
    while ((str = reader.readLine()) != null) {
        sb.append(str);
    }
    return Response.status(Response.Status.OK).entity(sb.toString()).build();
}
upload.txt
{
  "documents": [
    {
      "id": "1",
      "text": "abc"
    }
  ]
}
因此,基本上,我将sb.toString()发送回响应体。
我的问题是,如何从上面获取的响应中提取只有内容数据?
我尝试使用MultipartFile,但仍然没有成功。
英文:
I want to parse request body which accepts a file as multipart/form-data. Currently, when I try to print the inputStream as string, I get like below:
----------------------------559363225496099939024171
Content-Disposition: form-data; 
name="file"; 
filename="upload.txt"
Content-Type: text/plain
{  
    "documents": [    
        {      
            "id": "1",
            "text": "abc"    
        }  
     ]
}
----------------------------559363225496099939024171
Below is my controller.java
    @POST
    @Path("/modelInfo")
    @Produces({ "application/json" })
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response getModel(InputStream file) throws IOException {
        InputStreamReader isReader = new InputStreamReader(file, StandardCharsets.UTF_8);
        //Creating a BufferedReader object
        BufferedReader reader = new BufferedReader(isReader);
        StringBuffer sb = new StringBuffer();
        String str;
        while ((str = reader.readLine()) != null) {
            sb.append(str);
        }
   
        return Response.status(Response.Status.OK).entity(sb.toString()).build();
    }
upload.txt
{
  "documents": [
    {
      "id": "1",
      "text": "abc"
    }
  ]
}
So basically, I am sending sb.toString() back to response body.
My question is, how to extract only the content data from the above response I am getting?
I tried using MultipartFile, still no luck
答案1
得分: 0
@POST
@Path("/modelInfo")
@Produces({ "application/json" })
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response getModel(@RequestParam("file") MultipartFile file) throws IOException {
InputStreamReader isReader = new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8);
...
英文:
The rest endpoint method parameter should be of type MultipartFile then you can get the file input stream from that object
    @POST
    @Path("/modelInfo")
    @Produces({ "application/json" })
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response getModel(@RequestParam("file") MultipartFile file) throws IOException {
        InputStreamReader isReader = new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8);
    ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论