www.pudn.com > uploadaction.rar > CsReportImportAction.java
package com.baosight.bsfc.cs.action;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.MessageResources;
import com.baosight.bsfc.ct.po.TCtTemplateBO;
import com.baosight.common.global.CommonUtil;
import com.baosight.framework.config.FrameworkContext;
import com.baosight.framework.dao.DAO;
import com.baosight.framework.exception.ApplicationException;
public class CsReportImportAction extends Action {
TCtTemplateBO bo = (TCtTemplateBO)FrameworkContext.getInstance().getBean("bo-get-excel");
//DAO dao1 = (DAO)FrameworkContext.getInstance().getBean("dao-TCsReport");
DAO dao = (DAO)FrameworkContext.getInstance().getBean("dao-TCsRptitemplate");
private Logger logger = Logger.getLogger(CsReportImportAction.class);
private static final SimpleDateFormat ORA_DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
private static java.io.File file = null;
/*public Collection doQuery1()//从t_cs_report查数据
{
Map map=new HashMap();
Collection record =dao1.query("query", map);
return record;
}*/
public Collection doNotNullQuery(String para[])//从t_cs_rptitemplate查数据
{
Map map=new HashMap();
logger.info("----------------------------------------------------------->"+para[0]+"<---------------------------------------------");
map.put("reportId", para[0]);
logger.info("----------------------------------------------------------->"+para[3]+"<---------------------------------------------");
map.put("reportType", para[3]);
Collection record =dao.query("queryNotNullExcel", map);
return record;
}
public Collection doNullQuery(String para[])//从t_cs_rptitemplate查数据
{
Map map=new HashMap();
logger.info("----------------------------------------------------------->"+para[0]+"<---------------------------------------------");
map.put("reportId", para[0]);
logger.info("----------------------------------------------------------->"+para[3]+"<---------------------------------------------");
map.put("reportType", para[3]);
Collection record =dao.query("queryNullExcel", map);
return record;
}
public void getnullData(String para[]){
Collection recordnull=null;
recordnull=this.doNullQuery(para);
Map maptemp=new HashMap();
Map maptexcel=new HashMap();
Iterator it=recordnull.iterator();
while(it.hasNext()){
try{
maptemp=(Map)it.next();
String serialNo=(String)maptemp.get("serial");
String itemName=(String)maptemp.get("itemName");
maptexcel.put("serialNo", serialNo);
maptexcel.put("itemName", itemName);
maptexcel.put("reportId", para[0]);
maptexcel.put("custId", para[1]);
maptexcel.put("periodId", para[2]);
//record.add(maptexcel);
bo.insert("insert",maptexcel);//增加单条记录
}catch(Exception e)
{
e.printStackTrace();
throw new ApplicationException(e);
}
}
}
public void getData(String para[])//得到excel中读金额数据和从前台过来的数据
{
//this.doQuery1();
Collection recordtemp=null;
// Map map=new HashMap();
Map maptemp=new HashMap();
Map maptexcel=new HashMap();
recordtemp=this.doNotNullQuery(para);
Iterator it=recordtemp.iterator();
while(it.hasNext()){
try{
maptemp=(Map)it.next();
String rowNo=(String)maptemp.get("rowNo");
String colNo=(String)maptemp.get("colNo");
String position1=(String)maptemp.get("position");
String position2=(String)maptemp.get("position2");
String serialNo=(String)maptemp.get("serial");
String itemName=(String)maptemp.get("itemName");
String codeNo=(String)maptemp.get("code");
if( (codeNo==null) ||codeNo.equals("")) {
codeNo="0";
}
maptexcel=ReadExcelMetaData.readExcel(position1, position2);
maptexcel.put("serialNo", serialNo);
maptexcel.put("itemName", itemName);
maptexcel.put("codeNo", codeNo);
maptexcel.put("rowNo", rowNo);
maptexcel.put("colNo", colNo);
maptexcel.put("reportId", para[0]);
maptexcel.put("custId", para[1]);
maptexcel.put("periodId", para[2]);
//record.add(maptexcel);
bo.insert("insert",maptexcel);//增加单条记录
}catch(Exception e)
{
e.printStackTrace();
throw new ApplicationException(e);
}
}
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MessageResources messages = getResources(request);
ActionErrors errors = new ActionErrors();
HttpSession session = request.getSession();
// 执行
try {
Date date = new Date();
SimpleDateFormat theDateTimeFormat = (SimpleDateFormat) ORA_DATE_TIME_FORMAT
.clone();
theDateTimeFormat.setLenient(false);
String fileNo = theDateTimeFormat.format(date);
System.out.println("++++++++++++++++++++++++++++++++++++++");
DynaActionForm uploadForm = (DynaActionForm) form;
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
response.setContentType("text/html; charset=utf-8");
}
FormFile filestream = (FormFile) uploadForm.get("uploadFile"); // 从Form类中获得上传的文件流
String fileName = filestream.getFileName(); // 获得文件名
// DAO dao = (DAO)
// FrameworkContext.getInstance().getBean("dao-TNbUseaccount");
// Collection results = dao.query("getFileNo", new HashMap());
// Iterator itr_results = results.iterator();
// if (itr_results.hasNext())
// {
// Map resultMap = (Map)itr_results.next();
// fileNo = (String)resultMap.get("fileNo");
// }
// 文件名为新生成文件号加上传文件后缀名
fileName = fileNo + fileName.substring(fileName.lastIndexOf("."));
// 判断文件的大小是否大于4M
if (filestream.getFileSize() < (4 * 1024000)) {
ByteArrayOutputStream filedata = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
InputStream stream = filestream.getInputStream();
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
filedata.write(buffer, 0, bytesRead);
}
stream.close();
file = new java.io.File(servlet.getServletContext()
.getRealPath("/")
+ "/temp" + "/" + fileName);
FileOutputStream fileOut = new FileOutputStream(file);
fileOut.write(filedata.toByteArray());
fileOut.close();
if (stream != null) {
stream.close();
}
if (fileOut != null) {
fileOut.close();
}
if (filedata != null) {
filedata.close();
}
} else {
// 提示用户上传文件不能大于4M
throw new ApplicationException("上传文件不能大于4M!");
}
logger.info("===============action结t束===================");
logger
.info("=================="
+ request.getParameter("reportId"));
logger.info("==================" + request.getParameter("custId"));
logger
.info("=================="
+ request.getParameter("reportType"));
logger
.info("=================="
+ request.getParameter("periodId"));
// ActionForward forward = new
// ActionForward("/cs/TCsReport.jsp?fileName="+fileName+"&reportId="+reportId+"&custId="+custId+"&periodId="+periodId);
String reportId = (String) request.getParameter("reportId");
//reportId = CommonUtil.fillString(reportId, '0', 2);
String custId = (String) request.getParameter("custId");
custId = CommonUtil.fillString(custId, '0', 4);
String periodId = (String) request.getParameter("periodId");
String reportType=(String) request.getParameter("reportType");
logger.info("reportType"+reportType);
//reportType = CommonUtil.fillString(reportType, '0', 1);
logger.info("reportId=====================" + reportId);
ActionForward forward = new ActionForward(
"/pages/cs/TCsReport.jsp?fileName=" + fileName + "&reportId="
+ reportId + "&custId=" + custId + "&periodId="
+ periodId+"&reportType="+reportType);
logger.info("===============path==================="
+ forward.getPath());
String para[]={reportId,custId,periodId,reportType};
getData(para);
this.getnullData(para);
return mapping.findForward("successExcel");
} catch (Exception e) {
e.printStackTrace();
throw new ApplicationException(e.getMessage());
}
}
public static File getFile() {
return file;
}
}