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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:17:42  来源:igfitidea点击:

how to get the inputStream from Workbook object in java

javaexcelapache-poi

提问by aradhya

When I upload the file i am passing the inputstream to the workbook. Now I want to use this InputStreamfrom workbook in another method like save where I save the file InputStreamin 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 inptestvariable accepts InputStreamwhich 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 用于多种目的的方法:

  1. To create a Workbook object (which you're already doing)
  2. To save the content of that InputStream somewhere else
  1. 创建一个 Workbook 对象(您已经在做)
  2. 将该 InputStream 的内容保存在其他地方

Since reading from an InputStream is usually a one-time-only operation that cannot be repeated, then you can do the following:

由于从 InputStream 读取通常是不能重复的一次性操作,因此您可以执行以下操作:

  1. Save the full content of the InputStream to a buffer.
  2. Open two new InputStreams from the buffer.
  3. Pass your InputStreams to your two methods.
  1. 将 InputStream 的全部内容保存到缓冲区。
  2. 从缓冲区打开两个新的 InputStreams。
  3. 将您的 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();