网页版发布
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
package com.cpic.telsale.DataImport;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/28.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.*;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.apache.poi.hssf.*;
|
||||
import org.apache.poi.hssf.usermodel.*;
|
||||
import org.apache.poi.ss.usermodel.CellType;
|
||||
|
||||
import com.cpic.util.staff.*;
|
||||
|
||||
public class DataImport
|
||||
{
|
||||
private DataImport()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取xls表格插入数据,要么全部成功,要么全部失败。
|
||||
*
|
||||
* @param fileName xls文件名
|
||||
* @param telsalePolicyOperatorListTableName informix库电销转介绍表名
|
||||
* @param informixURL informix数据库URL
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException 数据库异常
|
||||
* @throws XlsFileFormatException xls文件格式错误异常
|
||||
*/
|
||||
public static String importTelsalePolicyDataFromXlsx( String fileName,
|
||||
String telsalePolicyOperatorListTableName,
|
||||
String informixURL ) throws
|
||||
IOException,
|
||||
ClassNotFoundException,
|
||||
SQLException,
|
||||
XlsFileFormatException
|
||||
{
|
||||
String importResult = "";
|
||||
String importMessage = null;
|
||||
int rowCount = 1; //excel文件行索引
|
||||
int successCount = 0; //成功写入的行计数
|
||||
int failCount = 0; //写入失败的行计数
|
||||
|
||||
//excel相关
|
||||
FileInputStream xlsFileIn = null;
|
||||
HSSFWorkbook xlsFile = null;
|
||||
HSSFSheet sheet = null;
|
||||
HSSFRow row = null;
|
||||
|
||||
try
|
||||
{
|
||||
xlsFileIn = new FileInputStream( fileName );
|
||||
xlsFile = new HSSFWorkbook( xlsFileIn );
|
||||
sheet = xlsFile.getSheetAt( 0 ); //第一个sheet
|
||||
row = sheet.getRow( rowCount );
|
||||
}
|
||||
catch( IOException error ) //有文件格式错误的可能。
|
||||
{
|
||||
xlsFile.close();
|
||||
xlsFileIn.close();
|
||||
|
||||
throw new XlsFileFormatException( "文件格式错误,请检查是否xls格式文件。" );
|
||||
}
|
||||
|
||||
//informix相关
|
||||
Class.forName( "com.informix.jdbc.IfxDriver" );
|
||||
String ifxSql =
|
||||
"INSERT INTO " + telsalePolicyOperatorListTableName + " \n" +
|
||||
" (bdh,\n" +
|
||||
" zhjywy,\n" +
|
||||
" zhjywymc,\n" +
|
||||
" kshdm,\n" +
|
||||
" kshmc,\n" +
|
||||
" zhjywybm,\n" +
|
||||
" zhjywybmm,\n" +
|
||||
" chshdm,\n" +
|
||||
" khmc,\n" +
|
||||
" chph,\n" +
|
||||
" khjl,\n" +
|
||||
" khjllx,\n" +
|
||||
" jjbj,\n" +
|
||||
" czydm,\n" +
|
||||
" czrq)\n" +
|
||||
"VALUES\n" +
|
||||
" (?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" ?,\n" +
|
||||
" 0,\n" +
|
||||
" 0,\n" +
|
||||
" 1,\n" +
|
||||
" '800',\n" +
|
||||
" today)";
|
||||
Connection ifxConn = DriverManager.getConnection( informixURL );
|
||||
PreparedStatement ifxStmt = ifxConn.prepareStatement( ifxSql );
|
||||
|
||||
ifxConn.setAutoCommit( false );//关闭自动提交,整个表格要么全部成写入,要么全部失败。
|
||||
|
||||
//数据相关
|
||||
String policyNo = null;
|
||||
String operatorCode = null;
|
||||
StaffInfo staff = null;
|
||||
boolean isSuccess = true; //标志位,用来指示在保存过程中是否出现错误。
|
||||
|
||||
while ( row != null )
|
||||
{
|
||||
policyNo = row.getCell( 1 ).getStringCellValue().trim();
|
||||
operatorCode = getStringValueFromHSSFCell( row.getCell( 4 ) );
|
||||
|
||||
//判断一下是不是空的单元格
|
||||
if ( policyNo.length() == 0 || operatorCode.length() == 0 )
|
||||
{
|
||||
importResult = importResult + "第" + rowCount + "行数据为空。<br>";
|
||||
|
||||
isSuccess = false;
|
||||
}
|
||||
else //不是空单元格,进行处理
|
||||
{
|
||||
try
|
||||
{
|
||||
//取得经办人信息
|
||||
staff = new StaffInfo( operatorCode );
|
||||
|
||||
//写入informix
|
||||
ifxStmt.setString( 1, policyNo );
|
||||
ifxStmt.setString( 2, staff.getStaffCode() );
|
||||
ifxStmt.setString( 3, staff.getStaffName() );
|
||||
ifxStmt.setString( 4, staff.getSectionOfficeCode() );
|
||||
ifxStmt.setString( 5, staff.getSectionOfficeName() );
|
||||
ifxStmt.setString( 6, staff.getDepartmentCode() );
|
||||
ifxStmt.setString( 7, staff.getDepartmentName() );
|
||||
ifxStmt.setString( 8, getStringValueFromHSSFCell( row.getCell( 6 ) ) );
|
||||
ifxStmt.setString( 9, getStringValueFromHSSFCell( row.getCell( 3 ) ) );
|
||||
ifxStmt.setString( 10, getStringValueFromHSSFCell( row.getCell( 2 ) ) );
|
||||
|
||||
ifxStmt.execute();
|
||||
|
||||
successCount++;
|
||||
}
|
||||
catch( StaffCodeNotExistException error )
|
||||
{
|
||||
//经办人不存在
|
||||
importResult = importResult + "第" + rowCount + "行,保单号" + policyNo + ",经办人" + operatorCode + "不存在。<br>";
|
||||
|
||||
//标志位
|
||||
isSuccess = false;
|
||||
|
||||
//计数
|
||||
failCount++;
|
||||
}
|
||||
catch( SQLException error )
|
||||
{
|
||||
//写入错误
|
||||
importResult = importResult + "第" + rowCount + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>";
|
||||
|
||||
//标志位
|
||||
isSuccess = false;
|
||||
|
||||
//计数
|
||||
failCount++;
|
||||
}
|
||||
catch( Exception error )
|
||||
{
|
||||
//写入错误
|
||||
importResult = importResult + "第" + rowCount + "行,保单号" + policyNo + ",数据库写入错误,错误信息:" + error.getMessage() + "。<br>";
|
||||
|
||||
//标志位
|
||||
isSuccess = false;
|
||||
|
||||
//计数
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
rowCount++;
|
||||
row = sheet.getRow( rowCount );
|
||||
}
|
||||
|
||||
//判断标志位,如果为false,则说明在保存过程中出现了错误,放弃所有过程,进行回滚。
|
||||
if ( isSuccess == true )
|
||||
{
|
||||
//oracleConn.commit();
|
||||
ifxConn.commit();
|
||||
}
|
||||
else
|
||||
{
|
||||
ifxConn.rollback();
|
||||
}
|
||||
|
||||
//清理
|
||||
xlsFile.close();
|
||||
xlsFileIn.close();
|
||||
|
||||
//日志
|
||||
importMessage = "读取记录数量:" + (rowCount - 1) +
|
||||
"<br>正确记录数量:" + successCount +
|
||||
"<br>错误记录数量:" + failCount;
|
||||
|
||||
if ( failCount != 0 )
|
||||
{
|
||||
//有错误的记录,输出提示
|
||||
importMessage += "<br>请根据错误提示修正后重新导入!<br>";
|
||||
}
|
||||
else
|
||||
{
|
||||
//无错误的记录,输出成功提示
|
||||
importMessage += "<br>文件已成功导入!<br>";
|
||||
}
|
||||
|
||||
//追加导入记录
|
||||
importMessage += importResult;
|
||||
|
||||
return importMessage;
|
||||
}
|
||||
|
||||
private static String getStringValueFromHSSFCell( HSSFCell cell )
|
||||
{
|
||||
String value = null;
|
||||
|
||||
switch ( cell.getCellType() )
|
||||
{
|
||||
case HSSFCell.CELL_TYPE_NUMERIC:
|
||||
{
|
||||
//判断是不是整数
|
||||
double dValue = cell.getNumericCellValue();
|
||||
|
||||
if ( dValue == ( int )dValue )
|
||||
{
|
||||
value = String.valueOf( ( int )dValue );
|
||||
}
|
||||
else
|
||||
{
|
||||
value = String.valueOf( dValue );
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case HSSFCell.CELL_TYPE_STRING:
|
||||
{
|
||||
value = cell.getStringCellValue();
|
||||
break;
|
||||
}
|
||||
case HSSFCell.CELL_TYPE_BLANK:
|
||||
{
|
||||
value = "";
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
value = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package com.cpic.telsale.DataImport;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/29.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 文件格式错误异常
|
||||
*/
|
||||
public class XlsFileFormatException extends Exception
|
||||
{
|
||||
public XlsFileFormatException( String message )
|
||||
{
|
||||
super( message );
|
||||
}
|
||||
}
|
@@ -0,0 +1,70 @@
|
||||
package com.cpic.telsale.SessionListener;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/27.
|
||||
*/
|
||||
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.cpic.util.FileTools;
|
||||
|
||||
|
||||
public class SessionListener implements HttpSessionListener
|
||||
{
|
||||
@Override
|
||||
public void sessionCreated( HttpSessionEvent event )
|
||||
{
|
||||
HttpSession session = event.getSession();
|
||||
ServletContext context = session.getServletContext();
|
||||
|
||||
String sessionID = session.getId();
|
||||
String tempPathRoot = context.getRealPath( context.getInitParameter( "临时文件根目录" ) );
|
||||
String sessionTempPath = tempPathRoot + "/" + sessionID;
|
||||
|
||||
File tempDir = new File( sessionTempPath );
|
||||
|
||||
if ( !tempDir.exists() )
|
||||
{
|
||||
if ( tempDir.mkdirs() == true )
|
||||
{
|
||||
session.setAttribute( "临时文件根目录", sessionTempPath );
|
||||
}
|
||||
else
|
||||
{
|
||||
session.removeAttribute( "临时文件根目录" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sessionDestroyed( HttpSessionEvent event )
|
||||
{
|
||||
String tempPath = ( String ) event.getSession().getAttribute( "临时文件根目录" );
|
||||
|
||||
if ( tempPath == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
File tempDir = new File( tempPath );
|
||||
|
||||
if ( !tempDir.exists() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//删掉临时目录
|
||||
try
|
||||
{
|
||||
FileTools.deleteDirectory( tempPath );
|
||||
}
|
||||
catch( IOException error )
|
||||
{
|
||||
event.getSession().getServletContext().log( "删除“临时文件根目录”出错!" );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,139 @@
|
||||
package com.cpic.telsale.uploadData;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/27.
|
||||
*/
|
||||
|
||||
import org.apache.commons.fileupload.*;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import com.cpic.telsale.DataImport.*;
|
||||
|
||||
public class uploadDataProcessor extends HttpServlet
|
||||
{
|
||||
private String tempPathRoot; //临时文件目录
|
||||
// private String oracleURL;
|
||||
// private String oracleUserName;
|
||||
// private String oraclePassword;
|
||||
private String informixURL;
|
||||
private String telsalePolicyOperatorListTableName; //informix库电销转介绍表名
|
||||
|
||||
@Override
|
||||
public void init( ServletConfig config ) throws ServletException
|
||||
{
|
||||
//获取参数
|
||||
// oracleURL = ( String )config.getServletContext().getInitParameter( "oracleURL" );
|
||||
// oracleUserName = ( String )config.getServletContext().getInitParameter( "oracleUserName" );
|
||||
// oraclePassword = ( String )config.getServletContext().getInitParameter( "oraclePassword" );
|
||||
|
||||
//informix
|
||||
informixURL = ( String )config.getServletContext().getInitParameter( "informixURL" );
|
||||
telsalePolicyOperatorListTableName = (String)config.getServletContext().getInitParameter( "telsalePolicyOperatorListTableName" );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
|
||||
{
|
||||
tempPathRoot = ( String )request.getSession().getAttribute( "临时文件根目录" );
|
||||
String importMessage = null;
|
||||
|
||||
try
|
||||
{
|
||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||
|
||||
factory.setRepository( new File( tempPathRoot ) );
|
||||
|
||||
ServletFileUpload upload = new ServletFileUpload( factory );
|
||||
|
||||
List<FileItem> uploadFiles = upload.parseRequest( request );
|
||||
Iterator iterFiles = uploadFiles.iterator();
|
||||
|
||||
while ( iterFiles.hasNext() )
|
||||
{
|
||||
FileItem fileItem = ( FileItem )iterFiles.next();
|
||||
|
||||
if ( !fileItem.isFormField() )
|
||||
{
|
||||
//保存上传的文件,返回文件完整路径。
|
||||
String upLoadedFileName = saveUploadedFile( fileItem );
|
||||
|
||||
importMessage = DataImport.importTelsalePolicyDataFromXlsx( upLoadedFileName,
|
||||
telsalePolicyOperatorListTableName,
|
||||
informixURL );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( FileUploadException error )
|
||||
{
|
||||
//文件上传有异常,包装一下重新抛出
|
||||
throw new ServletException( error.getMessage() );
|
||||
}
|
||||
catch( XlsFileFormatException error )
|
||||
{
|
||||
//文件格式异常
|
||||
importMessage = error.getMessage();
|
||||
}
|
||||
catch( IOException error )
|
||||
{
|
||||
//包装一下重新抛出
|
||||
throw new ServletException( error.getMessage() );
|
||||
}
|
||||
catch( Exception error )
|
||||
{
|
||||
throw new ServletException( error.getMessage() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
request.getSession().setAttribute( "保存日志", importMessage );
|
||||
}
|
||||
|
||||
response.sendRedirect( "uploadPolicyData/uploadPolicyData.jsp" );
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存上传文件
|
||||
*
|
||||
* @param item
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private String saveUploadedFile( FileItem item ) throws Exception
|
||||
{
|
||||
String fileName = item.getName();
|
||||
|
||||
int fileNameIndex = 0;
|
||||
long fileSize = item.getSize();
|
||||
|
||||
//文件路径中可能有使用\和/作为目录分隔符,要分别判断
|
||||
fileNameIndex = fileName.lastIndexOf( "\\" );
|
||||
|
||||
if ( fileNameIndex == 0 )
|
||||
{
|
||||
fileNameIndex = fileName.lastIndexOf( "/" );
|
||||
}
|
||||
|
||||
//把文件名从路径中提取出来
|
||||
fileName = fileName.substring( fileNameIndex + 1, fileName.length() );
|
||||
|
||||
if ( fileName.equals( "" ) && fileSize == 0 )
|
||||
{
|
||||
throw new Exception( "上传文件错误!" );
|
||||
}
|
||||
|
||||
//生成最终的路径
|
||||
fileName = tempPathRoot + "/" + String.valueOf( Calendar.getInstance().getTimeInMillis() ) + fileName;
|
||||
|
||||
File uploadedFile = new File( fileName );
|
||||
|
||||
item.write( uploadedFile );
|
||||
|
||||
return fileName;
|
||||
}
|
||||
}
|
44
代码/jsp/telsale_import/src/com/cpic/util/FileTools.java
Normal file
44
代码/jsp/telsale_import/src/com/cpic/util/FileTools.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.cpic.util;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/27.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FileTools
|
||||
{
|
||||
private FileTools() {}
|
||||
|
||||
public static void deleteDirectory( String path ) throws IOException
|
||||
{
|
||||
File dir = new File( path );
|
||||
|
||||
if ( !dir.exists() )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//先删除子目录和文件
|
||||
File[] subFiles = dir.listFiles();
|
||||
int index = 0;
|
||||
|
||||
while ( index < subFiles.length )
|
||||
{
|
||||
if ( subFiles[index].isDirectory() )
|
||||
{
|
||||
deleteDirectory( subFiles[index].getAbsolutePath() );
|
||||
}
|
||||
|
||||
if ( !subFiles[index].delete() )
|
||||
{
|
||||
throw new IOException( "目录" + subFiles[index].getAbsolutePath() + "无法删除!" );
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
dir.delete();
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
package com.cpic.util.staff;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/28.
|
||||
*/
|
||||
public class StaffCodeNotExistException extends Exception
|
||||
{
|
||||
StaffCodeNotExistException( String message )
|
||||
{
|
||||
super( message );
|
||||
}
|
||||
}
|
107
代码/jsp/telsale_import/src/com/cpic/util/staff/StaffInfo.java
Normal file
107
代码/jsp/telsale_import/src/com/cpic/util/staff/StaffInfo.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.cpic.util.staff;
|
||||
|
||||
/**
|
||||
* Created by Kane on 2017/3/28.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.*;
|
||||
import java.text.ParseException;
|
||||
|
||||
public class StaffInfo
|
||||
{
|
||||
private String staffCode;
|
||||
private String staffName;
|
||||
private String sectionOfficeCode;
|
||||
private String sectionOfficeName;
|
||||
private String departmentCode;
|
||||
private String departmentName;
|
||||
|
||||
public StaffInfo( String staffCode ) throws StaffCodeNotExistException, ClassNotFoundException
|
||||
{
|
||||
if ( staffCode.length() < 3 )
|
||||
{
|
||||
throw new StaffCodeNotExistException( staffCode + "不存在。");
|
||||
}
|
||||
|
||||
String oracleURL = "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1";
|
||||
String oracleUserName = "idst0";
|
||||
String oraclePassword = "cpic123456";
|
||||
|
||||
this.staffCode = staffCode;
|
||||
|
||||
Class.forName( "oracle.jdbc.driver.OracleDriver" );
|
||||
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet result = null;
|
||||
|
||||
String sql =
|
||||
"SELECT ry.staff_name,\n" +
|
||||
" ksh.section_office_code,\n" +
|
||||
" ksh.section_office_name,\n" +
|
||||
" bm.department_code,\n" +
|
||||
" bm.department_name\n" +
|
||||
" FROM idst0.rydm_t ry,\n" +
|
||||
" idst0.ks_t ksh,\n" +
|
||||
" idst0.bm_t bm\n" +
|
||||
" WHERE ry.staff_code = ?\n" +
|
||||
" AND ry.department_code = bm.department_code\n" +
|
||||
" AND ry.section_office_code = ksh.section_office_code";
|
||||
|
||||
try
|
||||
{
|
||||
conn = DriverManager.getConnection( oracleURL, oracleUserName, oraclePassword );
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString( 1, staffCode );
|
||||
result = stmt.executeQuery();
|
||||
|
||||
if ( result.next() )
|
||||
{
|
||||
staffName = result.getString( "staff_name" );
|
||||
sectionOfficeCode = result.getString( "section_office_code" );
|
||||
sectionOfficeName = result.getString( "section_office_name" );
|
||||
departmentCode = result.getString( "department_code" );
|
||||
departmentName = result.getString( "department_name" );
|
||||
}
|
||||
|
||||
conn.close();
|
||||
}
|
||||
catch ( SQLException error )
|
||||
{
|
||||
//sql错误当做工号错误抛出
|
||||
throw new StaffCodeNotExistException( error.getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
public String getStaffCode()
|
||||
{
|
||||
return staffCode;
|
||||
}
|
||||
|
||||
public String getStaffName()
|
||||
{
|
||||
return staffName;
|
||||
}
|
||||
|
||||
public String getSectionOfficeCode()
|
||||
{
|
||||
return sectionOfficeCode;
|
||||
}
|
||||
|
||||
public String getSectionOfficeName()
|
||||
{
|
||||
return sectionOfficeName;
|
||||
}
|
||||
|
||||
public String getDepartmentCode()
|
||||
{
|
||||
return departmentCode;
|
||||
}
|
||||
|
||||
public String getDepartmentName()
|
||||
{
|
||||
return departmentName;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user