解析Spring中的文件上传

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年8月4日 20:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63246758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定