将 InputStream 转换为 byteArray[] 并将其分配给变量使 byteArray[] 为空

发布于 2025-01-12 19:20:28 字数 1259 浏览 0 评论 0原文

我有一个独立的 Spring BOOT 项目,它以 byteArray[] 形式接收上传的文件,并通过将其转换回 InputStream 并通过 Apache Tika & 解析来对此 byteArray[] 执行验证。删除元数据。

//Controller.java

byte[] byteArray = payload.getByteArray();
InputStream inputStream = FileUploadValidation.byteArrayToInputStream(byteArray);

InputStream finalStream = FileUploadValidation.checkIfDocumentIsValid(inputStream);
    byte[] finalByteArray = StreamUtils.copyToByteArray(finalStream); // This always comes empty
    return finalByteArray;



//Method in Utility Class (FileUploadValidation.java)  to perform operation on InputStream

public static InputStream checkIfDocumentIsValid(InputStream stream){

    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    ParseContext pcontext = new ParseContext();
    RarParser parser = new RarParser();
    parser.parse(stream, handler, metadata, pcontext);//This method simply parses the stream to Apache Tika PdfParser to check if it is a valid PDF and doesn't return anything.
    stripMetaData(metadata); //Removes metaData from the stream

    return stream;
}

我需要将 byteArray[] 返回到另一个 Spring 项目 我可以看到Controller.java中的finalStream确实不为空,但finalByteArray始终为空。 我做错了什么? 这是正确的做法吗? 上传的最大文件限制设置为 15MB。

I have a standalone Spring BOOT Project which receives uploaded file as an byteArray[] and I perform validations on this byteArray[] by converting it back to InputStream and parsing through Apache Tika & removing MetaData.

//Controller.java

byte[] byteArray = payload.getByteArray();
InputStream inputStream = FileUploadValidation.byteArrayToInputStream(byteArray);

InputStream finalStream = FileUploadValidation.checkIfDocumentIsValid(inputStream);
    byte[] finalByteArray = StreamUtils.copyToByteArray(finalStream); // This always comes empty
    return finalByteArray;



//Method in Utility Class (FileUploadValidation.java)  to perform operation on InputStream

public static InputStream checkIfDocumentIsValid(InputStream stream){

    BodyContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    ParseContext pcontext = new ParseContext();
    RarParser parser = new RarParser();
    parser.parse(stream, handler, metadata, pcontext);//This method simply parses the stream to Apache Tika PdfParser to check if it is a valid PDF and doesn't return anything.
    stripMetaData(metadata); //Removes metaData from the stream

    return stream;
}

I need to return the byteArray[] to another Spring Project
I can see that the finalStream in Controller.java indeed is not empty but the finalByteArray is always empty.
What am I doing wrong ?
Is it the right way to do it ?
Max File limit for uploading is set at 15MB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文