保存流對象(輸入流在第二次使用的時候會失效),在需要用到InputStream的地方再將字節數組轉化字節流使用,如果只需要用到一次inputstream流,就不用這樣啦,直接用就OK。
下面直接上代碼:
/**
* 保存流對象(輸入流在第二次使用的時候會失效)
* 在需要用到InputStream的地方再封裝成InputStream
* ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
* Workbook wb = new HSSFWorkbook(byteArrayInputStream);//byteArrayInputStream 繼承了InputStream,故這樣用並沒有問題
* 如果只需要用到一次inputstream流,就不用這樣啦,直接用就OK
* @param ins
*/
public byte[] saveaIns(InputStream ins){
byte[] buf = null;
try {
if(ins!=null){
buf = org.apache.commons.io.IOUtils.toByteArray(ins);//ins爲InputStream流
}
} catch (IOException e) {
e.printStackTrace();
}
return buf;
}
/**
* 保存導入的SMS信息(需要對信息進行檢驗)
* @return
*/
public void saveImportData(){
try{
byte[] excelByte = null;//保存excel二進制流數據
//從httpRequest中得到上載的文件對象
Object[] o = VVMappingUtil.populateByMutilpartRequest(this.getRequest());
FileItem fileItem = (FileItem)o[0]; //只有一個上載文件
HashMap<String,String> params = new HashMap<String,String>();//記錄文件中的orgname,用來檢驗文件中是否存在重複的記錄
params.put("version", "2003");//excel版本 爲 2003
HashMap<String, Object> smsnames = getAllName();//獲取所有廠商名稱(key:組織名稱 value:id)
params.put("smsnames", JSONObject.fromObject(smsnames).toString());//數據庫中的組織名稱
//object是List<Map<String,String>>類型
List<Object> settleobjsInfo = null;
ExcelInfo info = null;
try{
//解析並校驗excel內容
info = this.getExcelParams(fileItem);
excelByte = saveaIns(info.getIns());//用字節數組保存流對象(輸入流在第二次使用的時候會失效)
info.setIns(new ByteArrayInputStream(excelByte));
//判斷Excel是否爲兩頁
if(!sheetnumIsOk(new ByteArrayInputStream(excelByte))){
returnMsg.put("reason", true);
returnMsg.put("msg", "上傳的Excel應只有兩頁!");
this.writeRsp(VVMappingUtil.makeSuccessRsp(JSONObject.fromObject(returnMsg).toString())); return;
}
//判斷是否存在異常列,若存在則刪除
if(hasCheckColumn(fileItem,info)){
removeCkColumn(fileItem,info);
}
//判斷sheet頁中是否存在必要信息列
if(!sheetcolIsOk(new ByteArrayInputStream(excelByte))){
returnMsg.put("reason", true);
returnMsg.put("msg", "必要信息列不能修改或刪除!");
this.writeRsp(VVMappingUtil.makeSuccessRsp(JSONObject.fromObject(returnMsg).toString())); return;
}
//判斷Excel的sheet頁名稱是否爲:發送賬號列表,短信信息列表
if(!sheetnameIsOk(new ByteArrayInputStream(excelByte))){
returnMsg.put("reason", true);
returnMsg.put("msg", "上傳的Excel的sheet頁名稱應爲發送賬號列表,短信信息列表!");
this.writeRsp(VVMappingUtil.makeSuccessRsp(JSONObject.fromObject(returnMsg).toString())); return;
}
HashMap<String, Object> smsExcelNames = getExcelAllName(new ByteArrayInputStream(excelByte));//獲取所有《賬號發送列表》中的廠商名稱(key:組織名稱 value:id)
params.put("smsExcelNames", JSONObject.fromObject(smsExcelNames).toString());//《賬號發送列表》中的組織名稱
settleobjsInfo = (List<Object>) util.ExcelRecolve(info, params, smsbiz);
}catch(Exception ex){//捕獲解析excel過程中的異常信息,反饋給前臺,並終止導入操作
this.writeRsp(VVMappingUtil.makeFailureRsp(ex.getMessage()));
return;
}
if(settleobjsInfo != null && settleobjsInfo.size() > 0){
//判斷是否有錯誤信息,如果有,則不保存並且將文件返回前臺一個鏈接地址
for(int i=0;i<settleobjsInfo.size();i++){
@SuppressWarnings("unchecked")
List<Map<String,String>> data = (List<Map<String, String>>) settleobjsInfo.get(i);
int voLen = info.getSheetinfo()[i].getVoName().length;
int len = data.size();
for(int j=0;j<len;j++){
if(data.get(j).size()!=voLen+3){//返回的內容每一行存在三個額外字段(行號、列號、所在sheet名稱),所以要加3
//生成 短信通知_異常記錄.xls文件下載地址返回前臺
String filename = createAbnormalXls(settleobjsInfo,(User)this.getRequest().getSession().getAttribute("user"));
returnMsg.put("reason", false);
returnMsg.put("filename", filename+".xls");
this.writeRsp(VVMappingUtil.makeSuccessRsp(JSONObject.fromObject(returnMsg).toString())); return;
}
if(i==1&&j==data.size()-1){
//1:正確的.xls數據 2:所有廠商名稱和對應的電話號碼 3:當前登錄用戶信息
returnMsg.put("reason", true);
returnMsg.put("msg", "發送成功");
saveCorrectXls(settleobjsInfo,new ByteArrayInputStream(excelByte),(User)this.getRequest().getSession().getAttribute("user"));
}
}
}
}
String returnmsg = VVMappingUtil.makeSuccessRsp(JSONObject.fromObject(returnMsg).toString());
this.writeRsp(returnmsg);
} catch(Exception e){
this.writeRsp(VVMappingUtil.makeFailureRsp("發送失敗!"));
e.printStackTrace();
}
}
上面發送短信的業務代碼,需要多次用到文件流,保存文件流的具體實現就是saveaIns方法了。