您的位置:首页 > 数据库

使用SmartUpload上传文件并将读取的文件写入数据库

2013-11-01 12:24 435 查看
用到几个类:

1:DatabaseConnection

package com.upload.smartupload;

import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnection {

private	static final String DRIVER = "com.mysql.jdbc.Driver";
private	static final String DBNAME = "dyform";
private	static final String PASSWORD = "root";
private	static final String USERNAME = "root";
private	static final String URL = "jdbc:mysql://localhost:3307/" + DBNAME + "?rewriteBatchedStatements=true";
private	Connection conn = null;

//在构造方法中进行数据库的连接
public DatabaseConnection() throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver");
this.conn=conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
}catch(Exception e){
throw e;
}
}

public Connection getConnection(){
return this.conn;
}

public void close() throws Exception{
if(this.conn!=null){
try{
this.conn.close();
}catch(Exception e){
throw e;
}
}
}
}
2:UtilUUID

package com.upload.smartupload;

import java.util.UUID;

public class UtilUUID {
public UtilUUID() {
}
/**
* 获得一个UUID
* @return String UUID
*/
public static String getUUID(){
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
}
/**
* 获得指定数目的UUID
* @param number int 需要获得的UUID数量
* @return String[] UUID数组
*/
public static String[] getUUID(int number){
if(number < 1){
return null;
}
String[] ss = new String[number];
for(int i=0;i<number;i++){
ss[i] = getUUID();
}
return ss;
}

}


3:处理的jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="conf.jsp"%>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="com.upload.smartupload.IPTimeStamp" %>
<%@ page import="com.upload.smartupload.ReadFile" %>
<%@ page import="com.upload.smartupload.DatabaseConnection" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.upload.smartupload.UtilUUID" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>

<%
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
mySmartUpload.initialize(pageContext);
mySmartUpload.setMaxFileSize(10 * 1024*1024);
String parent = (String)session.getAttribute("parent");
//初始化
mySmartUpload.upload();
//循环取得所有上载的文件
String fileName = "";
long size = 0;

//只能上传指定格式的数据
if(mySmartUpload.getFiles().getFile(0).getFileName().matches("^\\w+\\.(dat)")){

for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//取得上载的文件
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (!file.isMissing())
{
//取得上载的文件的文件名
// fileName=file.getFileName();
size = file.getSize();
IPTimeStamp its=new IPTimeStamp(request.getRemoteAddr()); //实例化
String ext=mySmartUpload.getFiles().getFile(0).getFileExt();//取得文件后缀名
fileName=its.getIPTimeRand()+"."+ext; //重命名后的文件名
//保存路径
//将文件保存在服务器端
file.saveAs(parent+fileName,SmartUpload.SAVE_PHYSICAL);
out.println("<script>alert('文件上载成功!!!');</script>");
/**
*    读取文件操作
*/
//解析文件需要用到的路径参数
String str=new String(parent+fileName).replace("/", "//");    //替换后的文件名
//将文件读到流中
ArrayList<String> column3string = new ArrayList<String>();
ArrayList<String> column13string = new ArrayList<String>();
java.io.File files = new java.io.File(str);
if (files.isFile() && files.exists()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files));
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr, 1 * 1024 * 1024);//设读取文件的缓存为1MB
int count = 0;// 计数器
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lineTXT = null;
PreparedStatement pstmt = null;
String[] temp = null;
DatabaseConnection db=new DatabaseConnection();
Connection conn=db.getConnection();
conn.setAutoCommit(false);// 设置数据手动提交,自己管理事务
String sql="INSERT INTO dy_1381548830227 (LSH,FORMCODE,PARTICIPANT,CREATED,FATHERLSH,STATUSINFO,EXTENDATTRIBUTE,HIT,BELONGX,TIMEX,column3,column4,column5,column6,column7,column8,column9,column10,column11,column12,column13,column14) VALUES (?,?,?,?,?,null,null,null,null,null,?,null,null,null,null,null,null,null,null,null,?,null)";
pstmt = conn.prepareStatement(sql);
try {
while ((lineTXT = br.readLine()) != null) {
temp = lineTXT.split("	");

String str1=null;
//生成全球唯一的UUID
String[] ss = UtilUUID.getUUID(1);
for(int j=0;j<ss.length;j++){
str1=ss[j];
// System.out.println("打印出我要的数据:"+str1.toString());
}

String str2="admin".toString();
String str3="adminx".toString();
String str4=sdf.format(date).toString();
String str5="1".toString();
pstmt.setString(1,str1);
//	System.out.print("打印1"+str1);
pstmt.setString(2,str2);
//System.out.print("打印2"+str2);
pstmt.setString(3,str3);
//System.out.print("打印3"+str3);
pstmt.setString(4,str4);
//System.out.print("打印4"+str4);
pstmt.setString(5,str5);
//System.out.print("打印5"+str5);
pstmt.setString(6,temp[0]);
//System.out.print("打印6"+temp[0]);
pstmt.setString(7,temp[1]);
//System.out.print(temp[1]);
pstmt.addBatch();// 用PreparedStatement的批量处理
if (count % 50 == 0) {// 当增加了50个批处理的时候再提交
pstmt.executeBatch();// 执行批处理
conn.commit();
pstmt.clearBatch();
//打印插入的条数
//System.out.println("count: " + count);
}
count++;
}
pstmt.executeBatch();// 执行批处理
conn.commit();
pstmt.close();
conn.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}
else
{
out.println("<script>alert('文件上载失败!!!');</script>");
return;
}

}

}

else
{
out.println("<script>alert('上次失败,请上传考勤机中导出的数据!');</script>");
}
session.removeAttribute("parent");
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>考勤数据上传</title>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="Cache-Control" content="no-cache"/>
<meta http-equiv="Pragma" content="no-cache"/>
<link href="skin_1/style.css" type="text/css" rel="stylesheet"/>
</head>
<body class="htmlbody">
<p align=center><b>上传文件</b></p>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
<td width="30%" class="tablerow1">文件</td>
<td class="tablerow1">
<%=parent+fileName %>
</td>
</tr>
<tr>
<td width="30%" class="tablerow2">大小</td>
<td class="tablerow2">
<%=FileUtil.formatSize(size) %>
</td>
</tr>
</table>
<p align=center>
<input type=button class="button" value=" 关 闭 " onclick="window.opener.opener = null;window.opener.location.reload();window.close();"/>
</p>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: