将 InputStream 转换为 byteArray[] 并将其分配给变量使 byteArray[] 为空
我有一个独立的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论