如何将元素添加到具有模型类型的列表中

时间:2017-05-30 06:40:21

标签: java jersey multipartform-data apache-commons

我是java的新手。我想在列表中添加一个元素。

List<RequestAttachmentDTO> attachments

RequestAttachmentDTO类就在这里,

public class RequestAttachmentDTO {

    byte[] contentStream;
    String fileName;
    String contentType;
    String contentTransferEncoding;


    public RequestAttachmentDTO(byte[] contentStream, String fileName, String contentType) {
        this.contentStream = contentStream;
        this.fileName = fileName;
        this.contentType = contentType;
    }

    public RequestAttachmentDTO(byte[] contentStream, String fileName, String contentType,String contentTransferEncoding) {
        this.contentStream = contentStream;
        this.fileName = fileName;
        this.contentType = contentType;
        this.contentTransferEncoding=contentTransferEncoding;
    }

    public String getFileName() {
        return fileName;
    }

    public String getContentType() {
        return contentType;
    }

    public byte[] getContentStream() {
        return contentStream;
    }

    public String getContentTransferEncoding() {
        return contentTransferEncoding;
    }

}

这就是我试图添加的方式,

String fieldName = item.getFieldName();
            String fiileName = FilenameUtils.getName(item.getName());
            fileContent = item.getInputStream();                
            Path path = Paths.get("/data/uploads/form_urlencoded_simple_decoded_body.txt");
            byte[] data = Files.readAllBytes(path);

            List<RequestAttachmentDTO> attachments = new ArrayList<>();
            attachments.add(data,fieldName,"application/x-www-form-urlencoded");

它不接受它。

PS: - 文件item被视为来自multipart/form-data编码的JSP页面的上传。

您能帮我添加元素到此列表吗?谢谢。

1 个答案:

答案 0 :(得分:1)

欢迎使用Java!

目前您没有创建RequestAttachmentDTO的对象,为此您需要使用适当的值调用此构造函数RequestAttachmentDTO(byte[] contentStream, String fileName, String contentType)

因此,要解决此问题,请将此attachments.add(data,fieldName,"application/x-www-form-urlencoded");行更改为attachments.add(new RequestAttachmentDTO(data,fieldName,"application/x-www-form-urlencoded"));