【问题标题】:how do i add elements to a list with model type如何将元素添加到具有模型类型的列表中
【发布时间】:2017-05-30 06:40:21
【问题描述】:

我是 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 页面上传的。

你能帮我在这个列表中添加元素吗?谢谢。

【问题讨论】:

  • "String fileName =" ...你的代码有错别字吗?

标签: java jersey multipartform-data apache-commons


【解决方案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"));

【讨论】:

  • 非常感谢 :) :) 的详细分析。
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 2020-06-17
  • 2021-12-30
  • 1970-01-01
  • 2013-11-13
  • 2021-12-19
相关资源
最近更新 更多