admin管理员组

文章数量:1405532

Below is the data I am trying to save via a REST API

{
 "number": "123123",
 "priority": "2",
 "city": "",
 "notes": "",
 "attachments": [
      {
       "fileType":  "A",
       "fileName": "Receipt.pdf",
       "file": {}
      },
      {
       "fileType":  "D",
       "fileName": "Receipt1.pdf",
       "file": {}
      }
 ]
}

The "file" key contains uploaded file data.

The REST API is created using Jakarta.

The Rest API code looks like below:

import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import .jboss.resteasy.annotations.providers.multipart.MultipartForm;
import .jboss.resteasy.plugins.providers.multipart.InputPart;
import .jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
 @POST
 @Path(ResourcePaths.DATA_RESOURCE)
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 @Produces(MediaType.APPLICATION_JSON)
 public Response process_document(
            @MultipartForm MultipartFormDataInput input,
            @Context HttpServletRequest httpRequest
    ) {
        try (CloseableContext context = super.setContext(httpRequest)) {
            Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
            
            List<InputPart> attachmentParts = uploadForm.get("attachments");
            List<InputPart> numberData = uploadForm.get("number");

            logger.info("numberData ========> " + numberData);
            List<String> uploadedFiles = new ArrayList<>();

            if (attachmentParts != null) {
                for (InputPart inputPart : attachmentParts) {
                    Attachments attachmentFile = inputPart.getBody(Attachments.class, null);
                    logger.info("FileType ========> " + attachmentFile.getFileType());
                    logger.info("FileName ========> " + attachmentFile.getFileName());
                }
            } else {
                logger.info("attachmentParts is null");
            }
            return null;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}

Attachments class:

import java.io.InputStream;

public class Attachments {
    private String fileName;

    private String fileType;

    private InputStream file;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public InputStream getFile() {
        return file;
    }

    public void setFile(InputStream file) {
        this.file = file;
    }
}

When invoked, it prints only two lines:

numberData ========> 123123
attachmentParts is null

To debug, I tried to traverse uploadForm using the below code:

for (var entry : uploadForm.entrySet()) {
       logger.info("Keys ========> " + entry.getKey());
}

This gave me the following output:

Keys ========> number
Keys ========> priority
Keys ========> city
Keys ========> notes
Keys ========> attachments[0][fileType]
Keys ========> attachments[0][fileName]
Keys ========> attachments[0][file]
Keys ========> attachments[1][fileType]
Keys ========> attachments[1][fileName]
Keys ========> attachments[1][file]

Considering there can be more than 10 attachments sometimes, how can I fetch the details of all uploaded files? Is the written code good for scanning multiple attachments?

本文标签: