Recently, I participated in the maintenance of an online direct report project. This online direct report application has the function of importing text documents to the background database to batch import direct report users, including user information, such as organization name and enterprise qualification level. The core of this section is how to obtain the content in the uploaded text document. This section briefly introduces the program as follows:
First, the background JavaBean program is as follows:
Package util;
Import javax. servlet .*;
Import javax. servlet. http. httpservletrequest;
Import java. util. Collections list;
Import java. Io .*;
Public class readrequest {
Public referlist getrequest (httpservletrequest request ){
Pipeline list output = new pipeline list ();
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;
}
}
Then, write the corresponding JSP test page, which is not processed by the artist and is only used for testing :).
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> test </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 = "Submit">
<%
Pipeline list output = new pipeline list ();
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>
In this way, the content in the document will be output. Of course, there are some other content that can be processed based on actual needs. It should be noted that in this online direct reporting program, the document format is required. For each behavior, the basic information of a company is used between each attribute. ", "(or other symbols such as" @ "), and the order of each attribute is fixed (for example, the first item must be the legal person code, and the second item must be the company name ). After getting the content in the document, you can use a loop to insert user information in batches. For example, the content of the text document I uploaded is as follows:
300000000, Wuhan, 420101,230, A304, a211, 4700
300000001, Wuhan, 420101,230, A304, a211, 4700
300000002, Wuhan, 420101,230, A304, a211, 4700
300000003, Wuhan, 420101,230, A304, a211, 4700
First, the output is processed, and the content of the previous HTTP header is as follows: Content-Disposition: Form-data; name = "SDF"; filename = "C: /Documents and Settings/yy/desktop/test_jz.txt "and remove the tail. Now the output is only the content in the document. Then insert the database (only the code snippet is provided ):
// The following Code is applicable only to my own example.
For (INT I = 0; I <output. Size (); I ++ ){
If (output. Get (I) = NULL | (string) output. Get (I). Equals ("")){
Continue; // line feed automatically if empty rows exist}
String S = (string) output. Get (I); // obtain a line of 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.exe cuteupdate ();
}