A bar chart program was written at the company's request some time ago, but the content was obtained from another analysis software. The software can save the content generation table to the TXT file, I had to read the data by reading the file. After the program was written, I found that the client file could not be read after the program was put on the server ..... I found myself stupid .... There must be a problem with the path...
So I tried again, checked the information, and finally gave up, because no language can directly read client files from the server. To achieve this, I had to find another way. So I thought of the upload> Read> Delete method .....
Then start to construct the code ~ I tried it a little later and found that the file could not be deleted... But I immediately found the cause... You only need to close the output stream of the file before deleting the file. Because the file is in the read status, the file cannot be deleted, since I did not find a detailed method when I checked the relevant information on the Internet, I now think of contributing my own methods ....
Follow these steps:
========================================================== ==========================================================
I. First, we need to implement the file upload function. I have done this before, so it is quite handy.
1. Download The uploadbean component at the address below.
Http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
2. Go to the address below to go to The uploadbean plug-in -- filemover
Http://www.javazoom.net/jzservlets/uploadbean/uploadbeantools.html
3. Put the three packages cos. jar, uploadbean. jar, filemover. jar into the web program directory WEB-INF/lib
4. the upload configuration is complete.
Ii. form code for file upload selected by the customer
<Form name = Go Action = readtxt. jsp method = post ctype = "multipart/form-Data">
<Input type = file name = URL> <br>
<Input type = submit value = "Submit and read>">
</Form>
Iii. Achieve upload> Read> Save to database> Delete the main code readtxt. jsp [Note: I use UTF-8 encoding, specific to adjust according to the actual situation]
<% @ Page Language = "Java" pageencoding = "UTF-8" %>
<% @ Page import = "Java. Io .*,
Connect. Conn,
Javazoom. Upload .*,
Uploadutilities. filemover,
Java. util .*"
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> reading files </title>
</Head>
<Body>
<%
Request. setcharacterencoding ("UTF-8"); // you do not need to transcode one by one when setting the encoding format.
Filemover = new filemover (); // create a filemover object and instantiate filemover
Uploadbean upbean = new uploadbean (); // create an uploadbean object and instantiate upbean
Multipartformdatarequest mrequest = NULL;
Hashtable files = NULL; // create a container whose space is later used to store uploaded files
If (multipartformdatarequest. ismultipartformdata (request ))
{
Mrequest = new multipartformdatarequest (request, null, 100*1024*1024, multipartformdatarequest. cosparser, "UTF-8"); // note that the encoding parameters should also be set here to implement the multipart/form-Data
Files = mrequest. getfiles (); // get the file submitted in the form.
}
String swebrootpath = getservletcontext (). getrealpath ("/"); // obtain the root of the Web application.
String Spath = swebrootpath + "Temp"; // declare that the upload path is under the temp directory in the root directory
String sserverfilename = "";
String slocalfilename = "";
// File Retrieval
If (files! = NULL) | (! Files. isempty ()))
{
Uploadfile file = (uploadfile) files. Get ("url ");
Slocalfilename = file. getfilename ();
Int II = slocalfilename. indexof (".");
String sext = slocalfilename. substring (II, slocalfilename. Length (); // obtain the filename suffix.
// Get non-repeated file names. This step aims to prevent the upload of two TXT files with the same file name at the same time and avoid repeated file names.
Java. util. Date dt = new java. util. Date (system. currenttimemillis ());
Simpledateformat FMt = new simpledateformat ("yyyymmddhhmmsssss ");
Sserverfilename = FMT. Format (DT );
Sserverfilename = sserverfilename + sext;
// If this directory does not exist, create
File dir = new file (Spath );
If (! Dir. exists ())
{
Dir. mkdirs ();
}
Upbean. setfolderstore (Spath); // create the directory to be uploaded
Upbean. adduploadlistener (filemover); // added the filmover listener.
Filemover. setnewfilename (sserverfilename); // set the file name on the server
Upbean. Store (mrequest, "url"); // upload
}
Conn DB = new conn (); // connect to the database. This is my own JavaBean. In actual use, replace it with other database connection codes.
String SQL = "";
Filereader Fr = new filereader (Spath + "/" + sserverfilename); // create a filereader object and instantiate it as fr
Bufferedreader BR = new bufferedreader (FR); // create a bufferedreader object and instantiate it as Br
String line = Br. Readline (); // read a string from a file
While (reader. Ready ())
{
Line = Br. Readline ();
}
Out. Print (line); // output the content read from the file
// You can insert a filter on the read content and insert data using the following SQL statement.
SQL = ""; // write the SQL statement here
DB. Update (SQL); // execute the SQL statement
Fr. Close (); // close the file output stream. If it is not closed, the last step of deletion cannot be performed.
File txtfile = new file (Spath + "/" + sserverfilename); // create a file object and obtain the file path to be operated.
Txtfile. Delete (); // delete an object
Out. Print ("the file has been read ...");
%>
</Body>
</Html>