我是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
是从 JSP 页面以 multipart/form-data
编码上传的。
你能帮我向这个列表添加元素吗?谢谢。
最佳答案
欢迎使用 Java!
当前您尚未创建 RequestAttachmentDTO
对象,为此,您需要调用此构造函数 RequestAttachmentDTO(byte[] contentStream, String fileName, String contentType)
正确的值。
因此,要解决此问题,请将 attachments.add(data,fieldName,"application/x-www-form-urlencoded");
行更改为 attachments.add(new RequestAttachmentDTO(数据,fieldName,“application/x-www-form-urlencoded”));
关于java - 如何将元素添加到具有模型类型的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44255198/