java上传文件
<%@ page contentType="text/html; charset=GBK" %>
<%@ include file="config.jsp" %>
<%@ include file="CheckForm.jsp" %>
<%@ page import="unicom.*" %>
<%@ page import="seastar.*" %>
<%@ page import="java.sql.*" %>
<%
response.setHeader("expires", "0") ;
response.setHeader("Pragma","no-cache");
%>
<jsp:useBean id="employee" scope="session" class="unicom.employee" />
<jsp:useBean id="wintemplate" scope="page" class="unicom.WinTemplate" />
<jsp:useBean id="login" scope="session" class="unicom.login" />
<%
String wintemplateNAME;
wintemplateNAME="WP00000301";
%>
<%@ include file="CheckUser.jsp" %>
<%@ page import="java.sql.*,java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="unicom.*" %>
<%@ page import="seastar.*" %>
<%@ page import="java.sql.*,java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="jxl.Workbook" %>
<%@ page import="jxl.Sheet" %>
<%@ page import="com.jbsoft.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.regex.*" %>
<%
employee emp=(employee)session.getAttribute("employee");
//内容增加,文件上传组件
UploadFile uf = new UploadFile(request);
String fileName =ChangeCode.getByteISO_8859_1(uf.getFileName("upFile"));//文件名
System.out.println("-"+fileName+"-");
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
java.util.Date d = new java.util.Date();
String fileName2 = ft.format(d);
if (fileName.equals("") || fileName==null)
{
out.println("请选择文件!");
}else
{
//out.println(sel);
String path="D:\\jbsoft1\\tomcat-inside\\webapps\\WWW\\jsp\\upload\\";
//判断文件是否存在
File f=new File(path,fileName);
if(!(f.exists()))
{
try
{
uf.upload("upFile",path);
//记录入库
File file1 = new File(path+""+fileName);
File file2 = new File(path+""+fileName2);
file1.renameTo(file2);
try {
String errMsg ="";
InputStream is=new FileInputStream(path+fileName2);
Workbook wb=Workbook.getWorkbook(is);//获取EXCEL文件路径
Sheet sheet=wb.getSheet(0);//获取第一个工作薄
int rownum = sheet.getRows();
System.out.println("rownum="+rownum);
List<CustomerInfoModel> custs = new ArrayList();
List<String> errs = new ArrayList();
for(int i=1;i<rownum;i++){
CustomerInfoModel cm = cm = new CustomerInfoModel();
String str1 = sheet.getCell(0,i).getContents().trim();
String str2 = sheet.getCell(1,i).getContents().trim();
String str3 = sheet.getCell(2,i).getContents().trim();
String str4 = sheet.getCell(3,i).getContents().trim();
String str5 = sheet.getCell(4,i).getContents().trim();
Pattern p1 = Pattern.compile("^[0-9]*$");
Pattern p2 = Pattern.compile("^\\d{11}$");
if("".equals(str1)){
str1 = "身份证";
}
if("".equals(str2)){
System.out.println("主证件号码不能为空!");
errMsg = "<font color='red'>上传文件失败!第"+i+"行,2列主证件号码不能为空!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}else{
Matcher m1 = p1.matcher(str2);
if(!m1.find()){
errMsg = "<font color='red'>上传文件失败!第"+i+"行,2列主证件号码必须是数字!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
}
if("".equals(str3)){
System.out.println("业务号码不能为空!");
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,3列业务号码不能为空!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}else{
Matcher m2 = p2.matcher(str3);
if(m2.find()){
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,3列业务号码必须是数字!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
}
if("".equals(str4)){
System.out.println("客户姓名不能为空!");
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,4列客户姓名不能为空!</font>";
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
if("".equals(str5)){
str5 = "2023-12-31";
}
cm.setIdentitykind(str1);
cm.setIdentitycode(str2);
cm.setService_id(str3);
cm.setCust_name(str4);
cm.setCertificatemature(str5);
custs.add(cm);
}
out.println("上传文件成功!");
String sql = "insert into CUSTOMER_INFO_T values(?,?,?,?,?,?,?,?,sysdate)";
try {
dbconn instancedbconn = new dbconn();
Connection conn = instancedbconn.conn;
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement)conn.prepareStatement(sql);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if(custs!=null && custs.size()>0){
for(int i=0;i<custs.size();i++){
pst.setString(1,javautil.gb2iso(custs.get(i).getIdentitykind()));
pst.setString(2,custs.get(i).getIdentitycode());
pst.setString(3,custs.get(i).getService_id());
pst.setString(4,javautil.gb2iso(custs.get(i).getCust_name()));
pst.setString(5,"Y");
pst.setString(6,emp.id);
pst.setString(7,emp.AreaId);
java.util.Date date = format.parse("20"+custs.get(i).getCertificatemature());
pst.setDate(8,new java.sql.Date(date.getTime()));
pst.addBatch();
}
pst.executeBatch();
conn.setAutoCommit(true);
pst.close();
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
//instancedbconn.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}catch(Exception e){
out.println("上传文件失败");
}
}else{
out.println("文件已经存在,请重新上传!");
}
}
%>
<%@ include file="config.jsp" %>
<%@ include file="CheckForm.jsp" %>
<%@ page import="unicom.*" %>
<%@ page import="seastar.*" %>
<%@ page import="java.sql.*" %>
<%
response.setHeader("expires", "0") ;
response.setHeader("Pragma","no-cache");
%>
<jsp:useBean id="employee" scope="session" class="unicom.employee" />
<jsp:useBean id="wintemplate" scope="page" class="unicom.WinTemplate" />
<jsp:useBean id="login" scope="session" class="unicom.login" />
<%
String wintemplateNAME;
wintemplateNAME="WP00000301";
%>
<%@ include file="CheckUser.jsp" %>
<%@ page import="java.sql.*,java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="unicom.*" %>
<%@ page import="seastar.*" %>
<%@ page import="java.sql.*,java.text.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="jxl.Workbook" %>
<%@ page import="jxl.Sheet" %>
<%@ page import="com.jbsoft.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.regex.*" %>
<%
employee emp=(employee)session.getAttribute("employee");
//内容增加,文件上传组件
UploadFile uf = new UploadFile(request);
String fileName =ChangeCode.getByteISO_8859_1(uf.getFileName("upFile"));//文件名
System.out.println("-"+fileName+"-");
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
java.util.Date d = new java.util.Date();
String fileName2 = ft.format(d);
if (fileName.equals("") || fileName==null)
{
out.println("请选择文件!");
}else
{
//out.println(sel);
String path="D:\\jbsoft1\\tomcat-inside\\webapps\\WWW\\jsp\\upload\\";
//判断文件是否存在
File f=new File(path,fileName);
if(!(f.exists()))
{
try
{
uf.upload("upFile",path);
//记录入库
File file1 = new File(path+""+fileName);
File file2 = new File(path+""+fileName2);
file1.renameTo(file2);
try {
String errMsg ="";
InputStream is=new FileInputStream(path+fileName2);
Workbook wb=Workbook.getWorkbook(is);//获取EXCEL文件路径
Sheet sheet=wb.getSheet(0);//获取第一个工作薄
int rownum = sheet.getRows();
System.out.println("rownum="+rownum);
List<CustomerInfoModel> custs = new ArrayList();
List<String> errs = new ArrayList();
for(int i=1;i<rownum;i++){
CustomerInfoModel cm = cm = new CustomerInfoModel();
String str1 = sheet.getCell(0,i).getContents().trim();
String str2 = sheet.getCell(1,i).getContents().trim();
String str3 = sheet.getCell(2,i).getContents().trim();
String str4 = sheet.getCell(3,i).getContents().trim();
String str5 = sheet.getCell(4,i).getContents().trim();
Pattern p1 = Pattern.compile("^[0-9]*$");
Pattern p2 = Pattern.compile("^\\d{11}$");
if("".equals(str1)){
str1 = "身份证";
}
if("".equals(str2)){
System.out.println("主证件号码不能为空!");
errMsg = "<font color='red'>上传文件失败!第"+i+"行,2列主证件号码不能为空!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}else{
Matcher m1 = p1.matcher(str2);
if(!m1.find()){
errMsg = "<font color='red'>上传文件失败!第"+i+"行,2列主证件号码必须是数字!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
}
if("".equals(str3)){
System.out.println("业务号码不能为空!");
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,3列业务号码不能为空!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}else{
Matcher m2 = p2.matcher(str3);
if(m2.find()){
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,3列业务号码必须是数字!</font>";
out.println(errMsg);
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
}
if("".equals(str4)){
System.out.println("客户姓名不能为空!");
errMsg = "<font color='red'>上传文件失败! 第"+i+"行,4列客户姓名不能为空!</font>";
if(file1.exists()){
file1.delete();
}
throw new Exception(errMsg);
}
if("".equals(str5)){
str5 = "2023-12-31";
}
cm.setIdentitykind(str1);
cm.setIdentitycode(str2);
cm.setService_id(str3);
cm.setCust_name(str4);
cm.setCertificatemature(str5);
custs.add(cm);
}
out.println("上传文件成功!");
String sql = "insert into CUSTOMER_INFO_T values(?,?,?,?,?,?,?,?,sysdate)";
try {
dbconn instancedbconn = new dbconn();
Connection conn = instancedbconn.conn;
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement)conn.prepareStatement(sql);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if(custs!=null && custs.size()>0){
for(int i=0;i<custs.size();i++){
pst.setString(1,javautil.gb2iso(custs.get(i).getIdentitykind()));
pst.setString(2,custs.get(i).getIdentitycode());
pst.setString(3,custs.get(i).getService_id());
pst.setString(4,javautil.gb2iso(custs.get(i).getCust_name()));
pst.setString(5,"Y");
pst.setString(6,emp.id);
pst.setString(7,emp.AreaId);
java.util.Date date = format.parse("20"+custs.get(i).getCertificatemature());
pst.setDate(8,new java.sql.Date(date.getTime()));
pst.addBatch();
}
pst.executeBatch();
conn.setAutoCommit(true);
pst.close();
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
//instancedbconn.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
}catch(Exception e){
out.println("上传文件失败");
}
}else{
out.println("文件已经存在,请重新上传!");
}
}
%>