java 如何从java中的Workbook对象获取inputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30538529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the inputStream from Workbook object in java
提问by aradhya
When I upload the file i am passing the inputstream to the workbook. Now I want to use this InputStream
from workbook in another method like save where I save the file InputStream
in to DB. Here is my code.
当我上传文件时,我将输入流传递给工作簿。现在我想InputStream
在另一种方法中使用工作簿中的这个,比如保存我将文件保存InputStream
到数据库的位置。这是我的代码。
public void FileUpload(FileUploadEvent event) throws ParseException {
UploadedFile item = event.getUploadedFile();
Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(item.getInputStream());
}
Now I want to make Workbook object as instance variable and pass to another method like below.
现在我想将 Workbook 对象作为实例变量并传递给另一个方法,如下所示。
public String save() throws SQLException, IOException{
fileId = dao.savefile(workbook,fileName);
}
In my savefile method
在我的保存文件方法中
InputStream inptest= **workbook.getStream**
ps.setBinaryStream(2,fin,fin.available());
So inptest
variable accepts InputStream
which I wanted to get it from Workbook.
所以inptest
变量接受InputStream
我想从工作簿中获取它。
回答by GreenGiant
It sounds like what you are asking for is a way to use the InputStream for multiple purposes:
听起来您要求的是一种将 InputStream 用于多种目的的方法:
- To create a Workbook object (which you're already doing)
- To save the content of that InputStream somewhere else
- 创建一个 Workbook 对象(您已经在做)
- 将该 InputStream 的内容保存在其他地方
Since reading from an InputStream is usually a one-time-only operation that cannot be repeated, then you can do the following:
由于从 InputStream 读取通常是不能重复的一次性操作,因此您可以执行以下操作:
- Save the full content of the InputStream to a buffer.
- Open two new InputStreams from the buffer.
- Pass your InputStreams to your two methods.
- 将 InputStream 的全部内容保存到缓冲区。
- 从缓冲区打开两个新的 InputStreams。
- 将您的 InputStreams 传递给您的两个方法。
Code might look like this:
代码可能如下所示:
public void FileUpload(FileUploadEvent event) throws ParseException {
UploadedFile item = event.getUploadedFile();
InputStream originalInputStream = item.getInputStream();
byte[] buffer = IOUtils.toByteArray(originalInputStream);
InputStream is1 = new ByteArrayInputStream(buffer);
InputStream is2 = new ByteArrayInputStream(buffer);
Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(is1);
}
InputStream inptest = is2;
ps.setBinaryStream(2,fin,fin.available());
Note: this uses Apache Commons IO libraryfor IOUtils.
注意:这使用IOUtils 的Apache Commons IO 库。
回答by csunday95
If you are trying to save the Workbook object to a file, there is a method write()which takes in an OutputStream. Saving to a file can then be accomplished by
如果您尝试将 Workbook 对象保存到文件中,则有一个方法write()接受一个 OutputStream。然后可以通过以下方式保存到文件
FileOutputStream fos = new FileOutputStream("path/to/file/[filename]");
workbook.write(fos);
fos.close();