我正在尝试将xls表导入数据库,因为当我上传文件时,上传的文件是ByteBuffer格式,我编写了一个读取为FileInputStream的逻辑。
现在如何将此 ByteBuffer 转换为 FileInputStream
这是我的代码
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
String encoding = System.getProperty("file.encoding");
String filename = Charset.forName(encoding).decode(fileBytes).toString();
filename = filename.replaceAll("\\r", "");
我尝试使用 ByteArrayInputStream() 对其进行强制转换,但看起来不起作用!
最佳答案
- 更改您的逻辑以使用
InputStream
而不是FileInputStream。
您不关心输入来自何处。 使用以下代码:
ByteArrayInputStream bais = new ByteArrayInputStream(buffer.array(), buffer.position(), buffer.limit());
并将 bais
传递给您现有的方法。
关于java - Java中如何将ByteBuffer转换为FileInputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004236/