我是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(data,fieldName,"application/x-www-form-urlencoded"));