java裡面將OutputStream轉化InputStream(struts 運用)
阿新 • • 發佈:2019-02-16
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; } }
如需轉載:註明文章出處