【问题标题】:How to get InputStream from MultipartFormDataInput?如何从 MultipartFormDataInput 获取 InputStream?
【发布时间】: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


【解决方案1】:

我解决了从 "org.jboss.resteasy.util.Base64.InputStream" 更改 InputStream 的问题 到“java.io.InputStream"

【讨论】:

    猜你喜欢
    • 2010-10-20
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2019-09-06
    • 2014-01-06
    • 1970-01-01
    相关资源
    最近更新 更多