java里面将OutputStream转化为InputStream
java里面有的时候并不需要将OutputStream保存为实际的文件,因为这个既浪费空间又浪费时间。就如在使用struts进行文件的下载时,可能下载的内容是临时动态生成的。要实现下载有两种方式(这里利用动态生成Excel,并提供下载为例子)
第一:当创建一个内容后,利用FileOutputStream把文件写入其中,这个FileOutputStream指向一个具体的文件如:E:\\ddd.xlsx等等。在通过FileInputStream保存的文件转化为输入流。
public XSSFWorkbook createWorkBook(ArrayList<Student> students) throws FileNotFoundException{
//创建一个WorkBook,对应一个Excel文件
XSSFWorkbook workbook = new XSSFWorkbook();
//在WorkBook中添加一个sheet,对应Excel
XSSFSheet sheet = workbook.createSheet();
//在sheet添加表头第0行,注意老版本poi对Excel的行数列数有限制short
XSSFRow row = sheet.createRow(0);
//创建单元格,并设置表头设置表头居中
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//创建一个居中表格
XSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(style);
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue("学号");
cell1.setCellStyle(style);
XSSFCell cell2 = row.createCell(2);
cell2.setCellValue("性别");
cell2.setCellStyle(style);
XSSFCell cell3 = row.createCell(3);
cell3.setCellValue("年龄");
cell3.setCellStyle(style);
for(int i= 0; i < students.size(); i ++){
XSSFRow curRow = sheet.createRow(i+1);
XSSFCell curCell = curRow.createCell(0);
curCell.setCellValue(students.get(i).getStuName());
XSSFCell curCell1 = curRow.createCell(1);
curCell1.setCellValue(students.get(i).getStuNo());
XSSFCell curCell2 = curRow.createCell(2);
curCell2.setCellValue(students.get(i).getStuSex());
XSSFCell curCell3 = curRow.createCell(3);
curCell3.setCellValue(students.get(i).getStuAge());
}
//第一种方式:先保存为文件
FileOutputStream out = new FileOutputStream("E:ddd.xlsx");
workbook.write(out);
//再转化为输入流
FileInputStream in = new FileInputStream("E:ddd.xlsx");
return workbook;
}
第二:利用ByteArrayOutputStream来做缓存,将动态生成的文件内容写入这个缓存,然后再将缓存中的数据转化为自己数组,再利用ByteArrayInputStream转化为输入流。这个过程,并不保存文件,节省了空间和时间。
package stuinfo.service.imp;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import stuinfo.entity.Student;
import stuinfo.service.FileUploadService;
import stuinfo.service.StudentService;
import com.opensymphony.xwork2.ActionContext;
public class FileUploadServiceBean implements FileUploadService{
/**
*
* <p>description:根据条件查询信息</p>
* @author AbnerLi
* @date 2017年9月28日下午7:33:01
* @param context
* @return
*/
@Override
public ArrayList<Student> getDatas(ActionContext context){
StudentService service = new StudentServiceBean();
Map<String, Object> session = context.getSession();
//从session中取得条件
String property = (session.get("property") == null) ? null: (String)session.get("property");
String includeOrEqule = (session.get("includeOrEqule") == null) ? null: (String)session.get("includeOrEqule");
String condition = (session.get("condition") == null) ? null: (String)session.get("condition");
return service.findStudentsByConditions(property, includeOrEqule, condition);
}
/**
*
* <p>description:创建WorkBook对象</p>
* @author AbnerLi
* @date 2017年9月29日上午8:54:28
* @param students
* @return
*/
@Override
public XSSFWorkbook createWorkBook(ArrayList<Student> students){
//创建一个WorkBook,对应一个Excel文件
XSSFWorkbook workbook = new XSSFWorkbook();
//在WorkBook中添加一个sheet,对应Excel
XSSFSheet sheet = workbook.createSheet();
//在sheet添加表头第0行,注意老版本poi对Excel的行数列数有限制short
XSSFRow row = sheet.createRow(0);
//创建单元格,并设置表头设置表头居中
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//创建一个居中表格
XSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(style);
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue("学号");
cell1.setCellStyle(style);
XSSFCell cell2 = row.createCell(2);
cell2.setCellValue("性别");
cell2.setCellStyle(style);
XSSFCell cell3 = row.createCell(3);
cell3.setCellValue("年龄");
cell3.setCellStyle(style);
for(int i= 0; i < students.size(); i ++){
XSSFRow curRow = sheet.createRow(i+1);
XSSFCell curCell = curRow.createCell(0);
curCell.setCellValue(students.get(i).getStuName());
XSSFCell curCell1 = curRow.createCell(1);
curCell1.setCellValue(students.get(i).getStuNo());
XSSFCell curCell2 = curRow.createCell(2);
curCell2.setCellValue(students.get(i).getStuSex());
XSSFCell curCell3 = curRow.createCell(3);
curCell3.setCellValue(students.get(i).getStuAge());
}
return workbook;
}
/**
*
* <p>
* description:将workbook对象转化为输入流:过程是利用ByteArrayOutputStream为缓存,在将此ByteArrayOutputStream转化为InputStream
* 利用到了ByteArrayOutputStream来做缓存,先将文件写入其中,然后将其转为字节数组,最后利用ByteArrayInputStream转为输入流,供后续使用
* </p>
* @author AbnerLi
* @date 2017年9月29日上午8:56:18
* @param students
* @return
*/
@Override
public InputStream createExcelStream(XSSFWorkbook workbook) {
InputStream in = null;
try
{
//临时
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建临时文件
workbook.write(out);
byte [] bookByteAry = out.toByteArray();
in = new ByteArrayInputStream(bookByteAry);
}
catch (Exception e)
{
e.printStackTrace();
}
return in;
}
}
如需转载:注明文章出处