一阐斜、數(shù)據(jù)報(bào)表jxl
jxl是一款java讀寫office——Excel文件的工具。通過java程序進(jìn)行Excel文件的讀寫操作。
操作Excel首先應(yīng)該明確操作過程中java針對(duì)Excel文件的對(duì)象分類
- 文件對(duì)象
- 表
- 單元格
- 其他(包括字體,樣式等)
二崇猫、Excel工具類制作
根據(jù)業(yè)務(wù)功能诅福,制作適用于項(xiàng)目的工具類
1.創(chuàng)建的文件對(duì)象需要在服務(wù)器端形成才可以下載匾委,大量浪費(fèi)服務(wù)器資源,因此創(chuàng)建的文件只需要獲取到流對(duì)象即可氓润,使用jxl提供的創(chuàng)建可寫工作表對(duì)象赂乐,綁定輸出流對(duì)象
/**
* 創(chuàng)建Excel文件流
* @return
*/
public static WritableWorkbook cWorkbook(OutputStream os){
try {
return Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
2.工作表創(chuàng)建時(shí),需要指定工作表名稱與其所在位置咖气,因此挨措,定義創(chuàng)建工作表方法,使其更符合中國(guó)人的開發(fā)習(xí)慣
/**
* 創(chuàng)建工作表
* @param b
* @param idx 工作表索引,從1開始
* @param name 工作表顯示名稱
* @return
*/
public static WritableSheet cSheet(WritableWorkbook b,int idx,String name){
return b.createSheet(name, idx-1);
}
3.工作表的數(shù)據(jù)展示需要依賴于Label對(duì)象進(jìn)行崩溪,根據(jù)中國(guó)人的習(xí)慣浅役,第一個(gè)單元格為1行1列,改造適用的創(chuàng)建單元格的方法
/**
* 創(chuàng)建單元格
* @param a 行
* @param b 列
* @param value 值
* @return
*/
public static Label cLabel(int a ,int b ,String value){
return new Label(b-1, a-1, value);
}
4.Label對(duì)象創(chuàng)建完畢后伶唯,需要將其添加到工作表對(duì)象中觉既,創(chuàng)建對(duì)應(yīng)的方法,以add的首字母a進(jìn)行命名
/**
* 添加Label到Sheet
* @param l label
* @param s sheet
*/
public static void aLabelToSheet(Label l,WritableSheet s){
try {
s.addCell(l);
} catch (Exception e) {
e.printStackTrace();
}
}
此處不要將兩個(gè)方法設(shè)置成一個(gè)方法乳幸,否則Label對(duì)象將無法進(jìn)行樣式修改
5.創(chuàng)建設(shè)置工作表單元格寬度與高度的方法瞪讼,用于調(diào)整工作表的寬和高。此處以工作表顯示單位為基準(zhǔn)粹断,寬度不進(jìn)行修改符欠,高度需要*20。注意設(shè)置寬度與高度不是綁定工作表瓶埋,而是設(shè)置基準(zhǔn)行與類希柿,默認(rèn)從0開始,修改成中國(guó)人的習(xí)慣养筒。
/**
* 設(shè)置工作表列寬
* @param s
* @param idx 索引從1開始
* @param width 字符寬度
*/
public static void sColumnSize(WritableSheet s,int idx,int width){
s.setColumnView(idx-1, width);
}
/**
* 設(shè)置工作表高度
* @param s
* @param idx 索引從1開始
* @param height 字符高度
*/
public static void sRowSize(WritableSheet s,int idx,int height){
try {
s.setRowView(idx-1, height*20);
} catch (Exception e) {
e.printStackTrace();
}
6.單元格合并操作在文件中很常見曾撤,設(shè)置對(duì)應(yīng)的工具方法,快速使用晕粪,以中國(guó)人的習(xí)慣進(jìn)行參數(shù)設(shè)置盾戴,從第A行,第B列合并到第C行兵多,第D列
/**
* 設(shè)置單元格合并
* @param a 起始單元格行
* @param b 起始單元格列
* @param c 終止單元格行
* @param d 終止單元格列
*/
public static void sMerge(WritableSheet s,int a,int b,int c,int d){
try {
s.mergeCells(b-1, a-1, d-1, c-1);
} catch (Exception e) {
e.printStackTrace();
}
}
7.單元格樣式是修飾的重點(diǎn),該操作可以根據(jù)個(gè)人喜好進(jìn)行設(shè)計(jì)
/**
* 設(shè)置單元格樣式
* @param l
* @param fontName 字體:字符串,如"黑體"
* @param fontSize 字號(hào):數(shù)字橄仆,如24
* @param colour 字體顏色:Colour常量
* @param bgColour 單元格背景色:Colour常量
* @param align 對(duì)齊模式:0-左剩膘;1-中;2-右
* @param borderStyle 邊框線樣式:字符串盆顾,如0000代表上下左右都不要邊框怠褐,
* 如1100代表上下要邊框,如0011代表左右要邊框您宪,如果0220代表左邊和下邊要粗邊框
*/
public static void sLabelStyle(
Label l,String fontName,int fontSize,
Colour colour,Colour bgColour,
int align,String borderStyle){
try {
if(colour == null) colour = Colour.BLACK;
if(bgColour == null) bgColour = Colour.WHITE;
WritableFont wf = new WritableFont(
//設(shè)置字體
WritableFont.createFont(fontName),
//設(shè)置字號(hào)
fontSize,
//設(shè)置加粗
WritableFont.NO_BOLD,
//設(shè)置傾斜
false,
//設(shè)置下劃線
UnderlineStyle.NO_UNDERLINE,
//設(shè)置字體顏色
colour);
WritableCellFormat wcf = new WritableCellFormat(wf);
//設(shè)置背景色
wcf.setBackground(bgColour);
//設(shè)置對(duì)其方式
wcf.setAlignment(Alignment.CENTRE);
//設(shè)置邊框
if(borderStyle != null && borderStyle.length() == 4){
char[] bs = borderStyle.toCharArray();
if(bs[0] == '1'){
wcf.setBorder(Border.TOP, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[0] == '2'){
wcf.setBorder(Border.TOP, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[1] == '1'){
wcf.setBorder(Border.BOTTOM, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[1] == '2'){
wcf.setBorder(Border.BOTTOM, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[2] == '1'){
wcf.setBorder(Border.LEFT, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[2] == '2'){
wcf.setBorder(Border.LEFT, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[3] == '1'){
wcf.setBorder(Border.RIGHT, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[3] == '2'){
wcf.setBorder(Border.RIGHT, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
}
l.setCellFormat(wcf);
} catch (Exception e) {
e.printStackTrace();
}
}
8.源碼
package org.sihai.qualitycontrol.util.jxl;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
//操作Excel文件工具類
public class JxlUtil {
/**
* 創(chuàng)建Excel文件流
* @return
*/
public static WritableWorkbook cWorkbook(OutputStream os){
try {
return Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 創(chuàng)建工作表
* @param b
* @param idx 工作表索引,從1開始
* @param name 工作表顯示名稱
* @return
*/
public static WritableSheet cSheet(WritableWorkbook b,int idx,String name){
return b.createSheet(name, idx-1);
}
/**
* 創(chuàng)建單元格
* @param a 行
* @param b 列
* @param value 值
* @return
*/
public static Label cLabel(int a ,int b ,String value){
return new Label(b-1, a-1, value);
}
/**
* 添加Label到Sheet
* @param l label
* @param s sheet
*/
public static void aLabelToSheet(Label l,WritableSheet s){
try {
s.addCell(l);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 設(shè)置工作表列寬
* @param s
* @param idx 索引從1開始
* @param width 字符寬度
*/
public static void sColumnSize(WritableSheet s,int idx,int width){
s.setColumnView(idx-1, width);
}
/**
* 設(shè)置工作表高度
* @param s
* @param idx 索引從1開始
* @param height 字符高度
*/
public static void sRowSize(WritableSheet s,int idx,int height){
try {
s.setRowView(idx-1, height*20);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 設(shè)置單元格合并
* @param a 起始單元格行
* @param b 起始單元格列
* @param c 終止單元格行
* @param d 終止單元格列
*/
public static void sMerge(WritableSheet s,int a,int b,int c,int d){
try {
s.mergeCells(b-1, a-1, d-1, c-1);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Map<Integer, Alignment> alignMap = new HashMap<Integer, Alignment>();
static{
alignMap.put(0, Alignment.LEFT);
alignMap.put(1, Alignment.CENTRE);
alignMap.put(2, Alignment.RIGHT);
}
/**
* 設(shè)置單元格樣式
* @param l
* @param fontName 字體:字符串,如"黑體"
* @param fontSize 字號(hào):數(shù)字奈懒,如24
* @param colour 字體顏色:Colour常量
* @param bgColour 單元格背景色:Colour常量
* @param align 對(duì)齊模式:0-左奠涌;1-中;2-右
* @param borderStyle 邊框線樣式:字符串磷杏,如0000代表上下左右都不要邊框溜畅,
* 如1100代表上下要邊框,如0011代表左右要邊框极祸,如果0220代表左邊和下邊要粗邊框
*/
public static void sLabelStyle(
Label l,String fontName,int fontSize,
Colour colour,Colour bgColour,
int align,String borderStyle){
try {
if(colour == null) colour = Colour.BLACK;
if(bgColour == null) bgColour = Colour.WHITE;
WritableFont wf = new WritableFont(
//設(shè)置字體
WritableFont.createFont(fontName),
//設(shè)置字號(hào)
fontSize,
//設(shè)置加粗
WritableFont.NO_BOLD,
//設(shè)置傾斜
false,
//設(shè)置下劃線
UnderlineStyle.NO_UNDERLINE,
//設(shè)置字體顏色
colour);
WritableCellFormat wcf = new WritableCellFormat(wf);
//設(shè)置背景色
wcf.setBackground(bgColour);
//設(shè)置對(duì)其方式
wcf.setAlignment(Alignment.CENTRE);
//設(shè)置邊框
if(borderStyle != null && borderStyle.length() == 4){
char[] bs = borderStyle.toCharArray();
if(bs[0] == '1'){
wcf.setBorder(Border.TOP, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[0] == '2'){
wcf.setBorder(Border.TOP, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[1] == '1'){
wcf.setBorder(Border.BOTTOM, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[1] == '2'){
wcf.setBorder(Border.BOTTOM, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[2] == '1'){
wcf.setBorder(Border.LEFT, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[2] == '2'){
wcf.setBorder(Border.LEFT, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
if(bs[3] == '1'){
wcf.setBorder(Border.RIGHT, BorderLineStyle.THIN,jxl.format.Colour.BLACK);
}else if(bs[3] == '2'){
wcf.setBorder(Border.RIGHT, BorderLineStyle.MEDIUM,jxl.format.Colour.BLACK);
}
}
l.setCellFormat(wcf);
} catch (Exception e) {
e.printStackTrace();
}
}
}
三慈格、導(dǎo)出報(bào)表到Excel文件
根據(jù)業(yè)務(wù)功能,制作業(yè)務(wù)層方法遥金,返回流對(duì)象浴捆,用于Action下載,而不要返回文件對(duì)象稿械。
1.創(chuàng)建業(yè)務(wù)層方法选泻,返回InputStream
2.創(chuàng)建字節(jié)數(shù)組輸出流對(duì)象,用于讀取創(chuàng)建出的Excel文件
3.讀取要組織成Excel文件的數(shù)據(jù)
4.創(chuàng)建待顯示數(shù)據(jù)對(duì)應(yīng)的Excel寫文件對(duì)象美莫,并進(jìn)行文件拼寫
參看源碼
5.將工作表對(duì)象寫出到流
6.進(jìn)行輸出流轉(zhuǎn)換輸入流對(duì)象页眯,否則struts2文件下載無法使用
四、文件下載
Struts2提供文件下載功能的快速開發(fā)格式茂嗓,可針對(duì)流數(shù)據(jù)進(jìn)行轉(zhuǎn)化餐茵,最終形成下載文件。
1.Action類中設(shè)置方法用于初始化下載文件對(duì)應(yīng)的流對(duì)象述吸,此處需要的是輸入流對(duì)象
//下載excel報(bào)表
public String downloadExcelBill() throws Exception{
//將要下載的內(nèi)容寫入downloadExcelStreamn中
xlsName = "采購(gòu)報(bào)表.xls";
downloadExcelStreamn = billEbi.getWriteExcelStream(bqm);
return "downloadExcelBill";
}
2.設(shè)置對(duì)應(yīng)的結(jié)果集忿族,并設(shè)置返回類型為流對(duì)象
3.設(shè)置返回流對(duì)象,需要設(shè)置返回流對(duì)象名蝌矛,并提供對(duì)應(yīng)輸入流對(duì)象getter方法
4.設(shè)置返回類型中的對(duì)象名
5.下載文件類型根據(jù)所下載文件不同道批,進(jìn)行設(shè)置
如果不清楚下載文件類型設(shè)置內(nèi)容,可以通過查看tomcat中核心web.xml獲取對(duì)應(yīng)的設(shè)置值
Tomcat安裝目錄下的conf目錄下的web.xml文件查找對(duì)應(yīng)類型
設(shè)置下載文件類型入撒,并設(shè)置對(duì)應(yīng)字符集
PS:記得聲明字符集為iso-8859-1
6.設(shè)置下載文件顯示名稱
7.Action類中定義getter方法隆豹,用于獲取文件名
記得文件名轉(zhuǎn)碼,否則無法顯示中文
8.在Action中初始化要顯示的文件名
9.初始化業(yè)務(wù)層提供的Excel文件流對(duì)象
10茅逮、文件下載源碼
- action
private InputStream downloadExcelStreamn;
public InputStream getDownloadExcelStreamn() {
return downloadExcelStreamn;
}
private String xlsName;
public String getXlsName() throws UnsupportedEncodingException {
//字符級(jí)要進(jìn)行過濾
//xlsName->byte[]->string
return new String(xlsName.getBytes("utf-8"),"iso8859-1");
}
//下載excel報(bào)表
public String downloadExcelBill() throws Exception{
//將要下載的內(nèi)容寫入downloadExcelStreamn中
xlsName = "采購(gòu)報(bào)表.xls";
downloadExcelStreamn = billEbi.getWriteExcelStream(bqm);
return "downloadExcelBill";
}
- service
package org.sihai.qualitycontrol.invoice.bill.business.ebo;
import java.awt.Font;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.imageio.ImageIO;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.sihai.qualitycontrol.invoice.bill.business.ebi.BillEbi;
import org.sihai.qualitycontrol.invoice.bill.dao.dao.BillDao;
import org.sihai.qualitycontrol.invoice.bill.vo.BillQueryModel;
import org.sihai.qualitycontrol.invoice.goods.vo.GoodsModel;
import org.sihai.qualitycontrol.invoice.orderdetail.vo.OrderDetailModel;
import org.sihai.qualitycontrol.util.jxl.JxlUtil;
public class BillEbo implements BillEbi{
static {
StandardChartTheme theme = new StandardChartTheme("unicode") {
public void apply(JFreeChart chart) {
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
super.apply(chart);
}
};
theme.setExtraLargeFont(new Font("宋體", Font.PLAIN, 20));
theme.setLargeFont(new Font("宋體", Font.PLAIN, 14));
theme.setRegularFont(new Font("宋體", Font.PLAIN, 12));
theme.setSmallFont(new Font("宋體", Font.PLAIN, 10));
ChartFactory.setChartTheme(theme);
}
private BillDao billDao;
public void setBillDao(BillDao billDao) {
this.billDao = billDao;
}
public List<Object[]> getBuyBill(BillQueryModel bqm) {
return billDao.getBuyBill(bqm);
}
public List<OrderDetailModel> getBuyBillDetail(BillQueryModel bqm) {
return billDao.getBuyBillDetail(bqm);
}
public void writeJFreeChartToOs(OutputStream os,BillQueryModel bqm) throws Exception{
List<Object[]> temp = billDao.getBuyBill(bqm);
//jfc->os
DefaultPieDataset dataSet = new DefaultPieDataset();
for(Object[] objs:temp){
GoodsModel gm = (GoodsModel)objs[0];
Long sum = (Long) objs[1];
dataSet.setValue(gm.getName(), new Double(sum));
}
//由數(shù)據(jù)生成圖形
JFreeChart jfc = ChartFactory.createPieChart("采購(gòu)報(bào)表", dataSet, true, false, false);
PiePlot plot = (PiePlot) jfc.getPlot();
plot.setLabelFont(new Font("SansSerif", 0, 12));
plot.setNoDataMessage("無查詢結(jié)果報(bào)表信息璃赡!");
plot.setCircular(true);
plot.setLabelGap(0.02D);
//jfc對(duì)象已經(jīng)存在
BufferedImage bi = jfc.createBufferedImage(500, 370);
ImageIO.write(bi, "PNG", os);
}
public InputStream getWriteExcelStream(BillQueryModel bqm) throws Exception {
List<Object[]> temp = billDao.getBuyBill(bqm);
//將excel文件寫入流中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
WritableWorkbook w = Workbook.createWorkbook(bos);
WritableSheet s = w.createSheet("總括", 0);
//設(shè)置單元格寬度
JxlUtil.sColumnSize(s, 1, 8);
JxlUtil.sColumnSize(s, 2, 8);
JxlUtil.sColumnSize(s, 3, 25);
JxlUtil.sColumnSize(s, 4, 25);
JxlUtil.sColumnSize(s, 5, 25);
//設(shè)置單元格高度
JxlUtil.sRowSize(s, 1, 15);
JxlUtil.sRowSize(s, 2, 37);
JxlUtil.sRowSize(s, 3, 6);
JxlUtil.sRowSize(s, 4, 23);
//設(shè)置合并單元格
JxlUtil.sMerge(s, 2,2,2,4);
JxlUtil.sMerge(s, 3,2,3,5);
Label lab22 = JxlUtil.cLabel(2, 2, "進(jìn)貨統(tǒng)計(jì)報(bào)表");
JxlUtil.sLabelStyle(lab22, "黑體", 24, Colour.BLACK, Colour.LIGHT_BLUE, 1, "2020");
JxlUtil.aLabelToSheet(lab22, s);
Label lab25 = JxlUtil.cLabel(2, 5, "不限");
JxlUtil.sLabelStyle(lab25, "黑體", 12, Colour.BLACK, Colour.LIGHT_BLUE, 1, "2002");
JxlUtil.aLabelToSheet(lab25, s);
Label lab32 = JxlUtil.cLabel(3, 2, "");
JxlUtil.sLabelStyle(lab32, "黑體", 1, Colour.BLACK, Colour.GRAY_25, 1, "2022");
JxlUtil.aLabelToSheet(lab32, s);
Label lab42 = JxlUtil.cLabel(4, 2, "編號(hào)");
JxlUtil.sLabelStyle(lab42, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2220");
JxlUtil.aLabelToSheet(lab42, s);
Label lab43 = JxlUtil.cLabel(4, 3, "廠商");
JxlUtil.sLabelStyle(lab43, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2220");
JxlUtil.aLabelToSheet(lab43, s);
Label lab44 = JxlUtil.cLabel(4, 4, "商品名");
JxlUtil.sLabelStyle(lab44, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2220");
JxlUtil.aLabelToSheet(lab44, s);
Label lab45 = JxlUtil.cLabel(4, 5, "數(shù)量");
JxlUtil.sLabelStyle(lab45, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2222");
JxlUtil.aLabelToSheet(lab45, s);
int row = 5;
int i = 0;
Long sumAll = 0L;
for(Object[] objs:temp){
GoodsModel gm = (GoodsModel)objs[0];
Long sum = (Long)objs[1];
//設(shè)置行高
JxlUtil.sRowSize(s, row+i, 19);
//創(chuàng)建所有單元格
Label lab_data_1 = JxlUtil.cLabel(row+i, 2, i+1+"");
JxlUtil.sLabelStyle(lab_data_1, "宋體", 14, Colour.BLACK, Colour.WHITE, 1, "0120");
JxlUtil.aLabelToSheet(lab_data_1, s);
Label lab_data_2 = JxlUtil.cLabel(row+i, 3, gm.getGtm().getSm().getName());
JxlUtil.sLabelStyle(lab_data_2, "宋體", 14, Colour.BLACK, Colour.WHITE, 1, "0110");
JxlUtil.aLabelToSheet(lab_data_2, s);
Label lab_data_3 = JxlUtil.cLabel(row+i, 4, gm.getName());
JxlUtil.sLabelStyle(lab_data_3, "宋體", 14, Colour.BLACK, Colour.WHITE, 1, "0110");
JxlUtil.aLabelToSheet(lab_data_3, s);
Label lab_data_4 = JxlUtil.cLabel(row+i, 5, sum.toString());
JxlUtil.sLabelStyle(lab_data_4, "宋體", 14, Colour.BLACK, Colour.WHITE, 1, "0112");
JxlUtil.aLabelToSheet(lab_data_4, s);
i++;
sumAll += sum;
}
//設(shè)置最后一行高度
JxlUtil.sRowSize(s, row+i , 25);
//設(shè)置最后一行的合并
JxlUtil.sMerge(s, row+i, 2, row+i, 4);
Label lab_tail_1 = JxlUtil.cLabel(row+i, 2, "總計(jì):");
JxlUtil.sLabelStyle(lab_tail_1, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2220");
JxlUtil.aLabelToSheet(lab_tail_1, s);
Label lab_tail_2 = JxlUtil.cLabel(row+i, 5, sumAll.toString());
JxlUtil.sLabelStyle(lab_tail_2, "黑體", 18, Colour.BLACK, Colour.WHITE, 1, "2222");
JxlUtil.aLabelToSheet(lab_tail_2, s);
w.write();
w.close();
//當(dāng)前excel文件的內(nèi)容已經(jīng)寫入到流os對(duì)象中,該流是一個(gè)輸出流
//表現(xiàn)層需要的是輸入流
//輸出流轉(zhuǎn)輸入流
//ByteArrayOutputStream->inputStream
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
return bis;
}
}
如果想獲取更多源碼或者視頻教程献雅,歡迎關(guān)注我的微信公眾號(hào)
好好學(xué)java
碉考,在公眾號(hào)里,回復(fù):java基礎(chǔ)挺身、html5侯谁、javaEE基礎(chǔ)、struts2、spring墙贱、redis热芹、luncene、oracle
等惨撇,將可獲得以上的優(yōu)質(zhì)視頻教程及源碼伊脓。