2005-08-05 来源:CSDN 作者:CMTobby
|
最近参与了一个网上直报项目的维护工作,该网上直报应用程序有一个功能就是通过导入文本文档的方式向后台数据库中批量导入直报用户,包括用户各项信息如单位名称、企业资质等级等等。其核心之处就是如何获取所上载文本文档中的内容,现将该部分程序简单介绍如下:
首先是后台的javabean程序如下所示:
|
package
Util;
import
javax.servlet.
*
;
import
javax.servlet.http.HttpServletRequest;
import
java.util.LinkedList;
import
java.io.
*
;
public
class
ReadRequest{
public
LinkedList getRequest(HttpServletRequest request){
LinkedList output
=
new
LinkedList();
try
{
ServletInputStream in
=
request.getInputStream();
int
len
=
request.getContentLength();
System.out.println (len);
byte
[]b
=
new
byte
[len];
in.read(b,
0
,len);
String str
=
new
String(b);
System.out.println (str);
BufferedReader con
=
new
BufferedReader(
new
StringReader(str));
String c
=
""
;
while
((c
=
con.readLine())
!=
null
){
output.add(c);
}
}
catch
(Exception e){e.printStackTrace();}
return
output;
}
}
然后是写相应的jsp测试页面,此页面没有经过美工处理,仅为测试之用:)。
index.jsp
<%
@ page contentType
=
"
text/html; charset=gb2312
"
language
=
"
java
"
import
=
"
java.sql.*
"
errorPage
=
""
%>
<!
DOCTYPE HTML PUBLIC
"
-//W3C//DTD HTML 4.01 Transitional//EN
"
"
http://www.w3.org/TR/html4/loose.dtd
"
>
<
jsp:useBean id
=
"
pn
"
scope
=
"
page
"
class
=
"
Util.ReadRequest
"
/>
<%--<
jsp:useBean id
=
"
pn
"
scope
=
"
request
"
class
=
"
Util.GetFile
"
/>--%>
<%
@ page
import
=
"
java.util.*
"
%>
<
html
>
<
head
>
<
meta http
-
equiv
=
"
Content-Type
"
content
=
"
text/html; charset=gb2312
"
>
<
title
>
测试
</
title
>
</
head
>
<
body
>
<
form name
=
"
form1
"
action
=
"
index.jsp
"
method
=
"
post
"
enctype
=
"
multipart/form-data
"
>
<
input name
=
"
sdf
"
type
=
"
file
"
><
input name
=
"
aa
"
type
=
"
submit
"
value
=
"
提交
"
>
<%
LinkedList output
=
new
LinkedList();
output
=
pn.getRequest(request);
//
output=pn.readHttpData(request);
System.out.println(output.size());
for
(
int
i
=
0
;i
<
output.size();i
++
){
out.println(output.get(i)
+
"
<br>
"
);
}
%>
</
form
>
</
body
>
</
html
>
这样就会输出文档中的内容来了,当然还有一些其他的内容,可根据实际需要对内容进行处理。需要注意的是,在该网上直报程序中,文档的格式是有要求的,每行为一个公司的基本信息,各个属性之间用“,”隔开(也可以是其他的符号如“@”),并且各个属性出现的顺序也是固定的(如必须第一项是法人代码,第二项是公司名称)。得到了文档中的内容后,就可以用一个循环批量的插入用户信息了。比如说,我上载的文本文档内容如下所示:
300000000,武汉,420101,230,A304,A211,4700
300000001,武汉,420101,230,A304,A211,4700
300000002,武汉,420101,230,A304,A211,4700
300000003,武汉,420101,230,A304,A211,4700
首先对output进行处理,把前面的http头内容如:Content-Disposition: form-data; name="sdf"; filename="C:\Documents and Settings\yy\桌面\test_jz.txt"以及尾remove掉,现在的output就只有文档中的内容了。然后就开始插入数据库(只给出代码片断):
//以下代码仅适合本人的例子,具体情况具体而定
for
(
int
i
=
0
;i
<
output.size();i
++
){
if
(output.get(i)
==
null
||
( (String) output.get(i)).equals(
""
)) {
continue
;
//
空行则自动换行 }
String s
=
(String) output.get(i);
//
取得一行 String[] ss = s.split(",");
String sql
=
"
insert into xt_user(f001,f002,f003,f004,f005,f006) values(?,?,?,?,?,?)
"
;
PreparedStatement ps
=
con.prepareStatement(sql);
ps.setString(
1
,ss[
0
]);
ps.setString(
2
,ss[
1
]);
ps.setString(
3
,ss[
2
]);
ps.setString(
4
,ss[
3
]);
ps.setString(
5
,ss[
4
]);
ps.setString(
6
,ss[
5
]);
ps.executeUpdate();
}