我是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 :(得分: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"));