Use the smartupload component in JSP to upload a file --
JSP's support for file uploading is not as good as PHP's support. It is directly implemented as a function, not as it can be implemented through components in ASP. JSPs can be implemented through JavaBean. However, there is no need to write an uploaded bean on our own. There are already many molding technologies on the Internet, and smartupload is one of them. However, smartupload first reads files to the server's memory. Therefore, uploading too many files (more than 100 MB) may cause problems. It is also a weakness in the US :)
On the submitted page, the smartupload component requires that <form action = "Upload. jsp" enctype = multipart/form-data method = post> be submitted using word throttling. The following is an example of upload.htm:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<! -- Saved from url = (0057)HTTP: /localhost: 8080/jspsmartfile/JSP/uploadtemplate. jsp-->
<HTML> <Meta content = "text/html; charset = gb2312" http-equiv = Content-Type>
<Meta content = "mshtml 5.00.2920.0" name = generator> <Body bgcolor = # e6e6e6> <br>
<Form action = "Upload. jsp" enctype = multipart/form-data method = post>
<Table>
<Tbody>
<Tr>
<TD> <font color = #000000 face = helv, helvetica size = 1> & nbsp; File
: & Nbsp; </font> & nbsp; <input size = 60 type = file name = "file"> </TD> </tr>
<Tr>
<Tr>
<TD> <font color = #000000 face = helv, helvetica size = 1> & nbsp; File
: & Nbsp; </font> & nbsp; <input size = 60 type = file name = "file1"> </TD> </tr>
<Tr>
<TD> <font color = #000000 face = helv, helvetica size = 1> & nbsp; File
: & Nbsp; </font> & nbsp; <input size = 60 type = text name = "text"> </TD> </tr>
<Tr>
<TD
Align = right> <input type = submit value = Send name = "send"> </TD> </tr> </tbody> </table> </form> </ body>
Let's take a look at the received page. After uploading the file to the server, we will directly store it in the database: Upload. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "com. jspsmart. Upload. *" %>
<% @ Page import = "dbstep. idbmanager2000. *" %>
<%
// RealSample Upload Bean
Com. jspsmart. Upload. smartupload mysmartupload = new COM. jspsmart. Upload. smartupload ();
// InitialInitial
Mysmartupload. initialize (pagecontext );
// SetSets the upload maximum value.
Mysmartupload. setmaxfilesize (500*1024*1024 );
// UploadFile Loading
Mysmartupload. Upload ();
// FollowRing to retrieve all uploaded files
For (INT I = 0; I <mysmartupload. getfiles (). getcount (); I ++ ){
// ObtainFile to be uploaded
Com. jspsmart. Upload. File myfile = mysmartupload. getfiles (). GetFile (I );
If (! Myfile. ismissing ())
{
// ObtainFile name to be uploaded
String myfilename = myfile. getfilename ();
// ObtainFile name without a suffix
String suffix = myfilename. substring (0, myfilename. lastindexof ('.'));
// ObtainSuffix
String ext = mysmartupload. getfiles (). GetFile (0). getfileext ();
// ObtainFile Size
Int filesize = myfile. getsize ();
// WarrantyStorage path
String AA = getservletcontext (). getrealpath ("/") + "JSP \\";
String trace = AA + myfilename;
// ObtainGet other parameters
String explain = (string) mysmartupload. getrequest (). getparameter ("text ");
String send = (string) mysmartupload. getrequest (). getparameter ("send ");
// SetFiles are stored on the server.
Myfile. saveas (trace, mysmartupload. save_physical );
// LowerSave the uploaded file to the database.
// SetReading files to the stream
Java. Io. File file = new java. Io. File (TRACE );
Java. Io. fileinputstream FCM = new java. Io. fileinputstream (File );
Out. println (file. Length ());
// InputOpen Database
Resultset result = NULL;
String msql = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
// SetFile written to database
Msql = "insert into marklist (markname, password, marksize, markdate, markbody) values (?,?,?,?,?) ";
Prestmt = dbaobj. Conn. preparestatement (msql );
Prestmt. setstring (1, "aaa1 ");
Prestmt. setstring (1, "0000 ");
Prestmt. setint (3, filesize );
Prestmt. setstring (4, dbaobj. getdatetime ());
Prestmt. setbinarystream (5, FS, (INT) file. Length ());
Dbaobj. Conn. setautocommit (true );
Prestmt.exe cuteupdate ();
Dbaobj. Conn. Commit ();
Out. println ("Upload successful !!! "). Tostring ());
}
Else
{Out. println ("Upload Failed !!! "). Tostring ());}
} // Corresponds to the preceding if
%>
Next, download is divided into two scenarios: 1. Download 2 directly from the database. Download from server
First, you can directly download the data from the database: Read the input stream from the database and convert it to a file.
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "dbstep. idbmanager2000. *" %>
<%
Int bytesum = 0;
Int byteread = 0;
// InputOpen Database
Resultset result = NULL;
String SQL = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
// ObtainObtain data in the database
SQL = "select * From t_local_zhongzhuan ";
Result = dbaobj. executequery (SQL );
Result. Next ();
// SetThe data in the database is read to the stream.
Inputstream instream = result. getbinarystream ("content ");
Fileoutputstream FS = new fileoutputstream ("C:/dffdsafd.doc ");
Byte [] buffer = new byte [1444];
Int length;
While (byteread = instream. Read (buffer ))! =-1)
{
Out. println ("<DT> <B>" + byteread + "</B> </DT> ");
Bytesum + = byteread;
System. Out. println (bytesum );
FS. Write (buffer, 0, byteread );
}
%>
In addition, the download from the server is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
String filename = "zsc104.swf". tostring ();
F // readTo stream
Inputstream instream = new fileinputstream ("C:/zsc104.swf ");
// SetOutput Format
Response. Reset ();
Response. setcontenttype ("bin ");
Response. addheader ("content-disposition", "attachment; filename = \" "+ filename + "\"");
// FollowLoop to retrieve data from a stream
Byte [] B = new byte [100];
Int Len;
While (LEN = instream. Read (B)> 0)
Response. getoutputstream (). Write (B, 0, Len );
Instream. Close ();
%>