解析Spring中的文件上传

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

Parsing File upload in Spring

问题

我想解析接受多部分/form-data文件的请求体。目前,当我尝试将inputStream打印为字符串时,我得到如下内容:

  1. ----------------------------559363225496099939024171
  2. Content-Disposition: form-data;
  3. name="file";
  4. filename="upload.txt"
  5. Content-Type: text/plain
  6. {
  7. "documents": [
  8. {
  9. "id": "1",
  10. "text": "abc"
  11. }
  12. ]
  13. }
  14. ----------------------------559363225496099939024171

以下是我的controller.java

  1. @POST
  2. @Path("/modelInfo")
  3. @Produces({ "application/json" })
  4. @Consumes(MediaType.MULTIPART_FORM_DATA)
  5. public Response getModel(InputStream file) throws IOException {
  6. InputStreamReader isReader = new InputStreamReader(file, StandardCharsets.UTF_8);
  7. //Creating a BufferedReader object
  8. BufferedReader reader = new BufferedReader(isReader);
  9. StringBuffer sb = new StringBuffer();
  10. String str;
  11. while ((str = reader.readLine()) != null) {
  12. sb.append(str);
  13. }
  14. return Response.status(Response.Status.OK).entity(sb.toString()).build();
  15. }

upload.txt

  1. {
  2. "documents": [
  3. {
  4. "id": "1",
  5. "text": "abc"
  6. }
  7. ]
  8. }

因此,基本上,我将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:

  1. ----------------------------559363225496099939024171
  2. Content-Disposition: form-data;
  3. name="file";
  4. filename="upload.txt"
  5. Content-Type: text/plain
  6. {
  7. "documents": [
  8. {
  9. "id": "1",
  10. "text": "abc"
  11. }
  12. ]
  13. }
  14. ----------------------------559363225496099939024171

Below is my controller.java

  1. @POST
  2. @Path("/modelInfo")
  3. @Produces({ "application/json" })
  4. @Consumes(MediaType.MULTIPART_FORM_DATA)
  5. public Response getModel(InputStream file) throws IOException {
  6. InputStreamReader isReader = new InputStreamReader(file, StandardCharsets.UTF_8);
  7. //Creating a BufferedReader object
  8. BufferedReader reader = new BufferedReader(isReader);
  9. StringBuffer sb = new StringBuffer();
  10. String str;
  11. while ((str = reader.readLine()) != null) {
  12. sb.append(str);
  13. }
  14. return Response.status(Response.Status.OK).entity(sb.toString()).build();
  15. }

upload.txt

  1. {
  2. "documents": [
  3. {
  4. "id": "1",
  5. "text": "abc"
  6. }
  7. ]
  8. }

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

  1. @POST
  2. @Path("/modelInfo")
  3. @Produces({ "application/json" })
  4. @Consumes(MediaType.MULTIPART_FORM_DATA)
  5. public Response getModel(@RequestParam("file") MultipartFile file) throws IOException {
  6. InputStreamReader isReader = new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8);
  7. ...

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:

确定