【发布时间】:2021-03-06 14:37:42
【问题描述】:
我正在尝试在 wildfly 中保存 pdf,我正在使用 Wildfly 20.0.1 提供的 RestEasy MultipartFormDataInput, 但它不起作用。
这就是我所拥有的:
public static Response uploadPdfFile(MultipartFormDataInput multipartFormDataInput) {
// local variables
MultivaluedMap<String, String> multivaluedMap = null;
String fileName = null;
InputStream inputStream = null;
String uploadFilePath = null;
try {
Map<String, List<InputPart>> map = multipartFormDataInput.getFormDataMap();
List<InputPart> lstInputPart = map.get("poc");
for(InputPart inputPart : lstInputPart){
// get filename to be uploaded
multivaluedMap = inputPart.getHeaders();
fileName = getFileName(multivaluedMap);
if(null != fileName && !"".equalsIgnoreCase(fileName)){
try {
// write & upload file to UPLOAD_FILE_SERVER
//here I have the error: Unable to find a MessageBodyReader for media type:
//application/pdf
inputStream = inputPart.getBody(InputStream.class,InputStream.class);
uploadFilePath = writeToFileServer(inputStream, fileName);
}catch (Exception e) {
e.printStackTrace();
}
// close the stream
inputStream.close();
}
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
// release resources, if any
}
return Response.ok("File uploaded successfully at " + uploadFilePath).build();
}
我正在使用邮递员进行测试,http POST 方法,在我发送的正文中:form-data - 文件并选择了 file.pdf。
当我发送请求时,我尝试下一个 RunTimeException:
inputStream = inputPart.getBody(InputStream.class,null);
我明白了:
java.lang.RuntimeException: RESTEASY007545: Unable to find a MessageBodyReader for media type: application/pdf and class type org.jboss.resteasy.util.Base64$InputStream
目前我正在将接收它的文件保存在 Base64 中,但我认为使用 MultipartFormDataInput 是正确的方法。
这是我调试时的样子:
感谢您的支持。
【问题讨论】:
-
您是否尝试过使用
application/octet-stream而不是application/pdf的内容类型?
标签: jax-rs wildfly multipartform-data resteasy