文件上传处理源代码:
private void savefile(PPBatchCancelDTO ppBatchCancelDTO, ByteArrayOutputStream os) {
File file=new File(ppBatchCancelDTO.getUploadFilePath());
FileOutputStream fileOutStream=null;
try {
fileOutStream = new FileOutputStream(file);
fileOutStream.write(os.toByteArray());
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
if(fileOutStream!=null){
try {
fileOutStream.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
private Map parseLine(String lineStr){
Map returnMap=new HashMap();
returnMap.put("returnCode", "00");
returnMap.put("returnMsg", "成功");
returnMap.put("ppSubOrder", null);
try {
Pattern pattern=Pattern.compile("(.{1,30}),([1,2,3])");
Matcher matcher=pattern.matcher(lineStr);
if(matcher.matches()){
String data=matcher.group(1);
String dataType=matcher.group(2);
PPSubOrderInfo ppSubOrderInfo=new PPSubOrderInfo();
if("1".equals(dataType)){
// 子订单编号
ppSubOrderInfo.setIdWltPPSubOrderInfo(data);
}else if("2".equals(dataType)){
// // 合作伙伴交易流水
ppSubOrderInfo.setTtNumber(data);
}else{
// 合作伙伴订单号
ppSubOrderInfo.setPartnerOrderId(data);
}
PPSubOrderInfo ppsuborder=this.getPPOrderInfoService().showPPSubOrderInfoDetail(ppSubOrderInfo);
if(ppsuborder!=null){
Map ms=new HashMap();
ms.put("subOrderInfoId",ppsuborder.getIdWltPPSubOrderInfo());//子订单
// 查询订单审核表 待审核
int listOfSub=ppOrderDAO().queryOrderAuditOfState03(ms);
if("05".equals(ppsuborder.getOrderState())){
returnMap.put("returnCode", "03");
returnMap.put("returnMsg", "订单已取消");
}else if(listOfSub>0){
returnMap.put("returnCode", "04");
returnMap.put("returnMsg", "订单已在审核列表中");
}else{
returnMap.put("ppSubOrder", ppsuborder);
}
}else{
returnMap.put("returnCode", "02");
returnMap.put("returnMsg", "订单不存在");
}
}else{
returnMap.put("returnCode", "01");
returnMap.put("returnMsg", "格式不正确");
}
} catch (Exception e) {
logger.error("解析批量取消上传文件异常",e);
returnMap.put("returnCode", "99");
returnMap.put("returnMsg", "解析批文件异常");
}
return returnMap;
}
/**
* 上载csv 处理
*/
public Map dealcsv(PPBatchCancelDTO ppBatchCancelDTO) throws BusinessServiceException {
Map resultMap=new HashMap();
logger.info("开始处理csv文件");
BufferedReader br = null;
InputStreamReader inputStreamReader = null;
ByteArrayOutputStream os = null;
ByteArrayInputStream bis = null;
int allcount=0;//记录总数
int recordCount=0;//成功插入数据
int errorCount=0;//失败数据
try {
ppBatchCancelDTO.setFileName(ppBatchCancelDTO.getLocalPath().substring(ppBatchCancelDTO.getLocalPath().lastIndexOf("\\")+1));
Integer countfile=this.getPPBatchCancelDAO().countPPBatchCancelByName(ppBatchCancelDTO);
if(countfile.intValue()>0){
throw new BusinessServiceException("文件名重复异常!");
}
os = ZipHelper.decompression(new ZipInputStream(
new ByteArrayInputStream(ppBatchCancelDTO.getStream())));
bis = new ByteArrayInputStream(os.toByteArray());
inputStreamReader = new InputStreamReader(bis);
//将上传的文件保存到nas目录
String uploadFileDs=PropertiesUtil.getResourceString("pp_batch_cancel.upload.refundfiles");
String uploadFileLogDs=PropertiesUtil.getResourceString("pp_batch_cancel.upload.refundLogfiles");
File uploadFileDsFile=new File(uploadFileDs);
if(!uploadFileDsFile.exists()){
uploadFileDsFile.mkdirs();
}
File uploadFileLogDsFile=new File(uploadFileLogDs);
if(!uploadFileLogDsFile.exists()){
uploadFileLogDsFile.mkdirs();
}
String fileSaveName=DateUtil.getDateForFormat("yyyyMMddHHmmss")+(int)(Math.random()*10000+1)+".csv";
String uploadFilePath=uploadFileDs+File.separator+fileSaveName;
String logPath=uploadFileLogDs+File.separator+"log_"+fileSaveName;
ppBatchCancelDTO.setUploadFilePath(uploadFilePath);
ppBatchCancelDTO.setLogFileName(logPath);
savefile(ppBatchCancelDTO, os);
br = new java.io.BufferedReader(inputStreamReader);
String str = "";
long startTime=System.currentTimeMillis();
FileOutputStream fos=new FileOutputStream(logPath);
Map allDataMap=new HashMap();
while ((str = br.readLine()) != null && !"".equals(str.trim())) {
allcount++;
String logStr=null;
Map parseMap=null;
try{
parseMap=parseLine(str);
if("00".equals(parseMap.get("returnCode"))){
PPSubOrderInfo ppSubOrderInfo=(PPSubOrderInfo)parseMap.get("ppSubOrder");
if(allDataMap.containsKey(ppSubOrderInfo.getIdWltPPSubOrderInfo())){
errorCount++;
logStr=str+",失败,与第"+allDataMap.get(ppSubOrderInfo.getIdWltPPSubOrderInfo())+"行数据重复\r\n";
fos.write(logStr.getBytes());
}else{
allDataMap.put(ppSubOrderInfo.getIdWltPPSubOrderInfo(), allcount);
recordCount++;
logStr=str+",成功,\r\n";
fos.write(logStr.getBytes());
}
}else{
logStr=str+",失败,"+parseMap.get("returnMsg")+"\r\n";
fos.write(logStr.getBytes());
errorCount++;
}
}catch(Exception e){
errorCount++;
logStr=str+",失败,处理数据出现异常\r\n";
fos.write(logStr.getBytes());
logger.error("处理数据出现异常,继续处理一条",e);
}
}
fos.flush();
fos.close();
if(allcount>500){
File uploadFile=new File(uploadFilePath);
if(!uploadFile.delete()){
logger.error("上载文件删除失败");
}
File logFile=new File(logPath);
if(!logFile.delete()){
logger.error("上载日志文件删除失败");
}
throw new BusinessServiceException("文件行数大于500条");
}else{
ppBatchCancelDTO.setAllCount(allcount);
ppBatchCancelDTO.setUploadSuccessCount(recordCount);
ppBatchCancelDTO.setUploadFailCount(errorCount);
this.getPPBatchCancelDAO().insertPPBatchCancel(ppBatchCancelDTO);
}
long endTime = System.currentTimeMillis();
logger.info("处理csv文件用时:"+(endTime-startTime));
} catch (FileNotFoundException e) {
logger.error("文件上传异常", e);
throw new BusinessServiceException("文件上载异常FileNotFoundException", e);
} catch (IOException e) {
logger.error("文件上传异常IOException", e);
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessServiceException("文件上载异常", e);
} catch (PafaDAOException e) {
logger.error("文件上传异常PafaDAOException", e);
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessServiceException("文件上载异常", e);
} catch (Exception e) {
logger.error("文件上传异常Exception", e);
// TODO Auto-generated catch block
e.printStackTrace();
throw new BusinessServiceException("文件上载异常", e);
} finally {
logger.error("关闭'上传文件'操作IO流");
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (inputStreamReader != null) {
inputStreamReader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bis!=null){
bis.close();
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
resultMap.put("allcount", allcount);
resultMap.put("recordCount", recordCount);
resultMap.put("errorCount", errorCount);
return resultMap;
}