2017-05-30 70 views
0

我是新來的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頁面上傳。

你能幫我添加元素到這個列表。謝謝。

+0

「字符串fiileName =」 ...有沒有在你的代碼一個錯字? – JonyD

回答

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

+0

非常感謝你:) :)的詳細anser。 – cmb28