JSP downloads uploaded files

Source: Internet
Author: User
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. *" %>
<%
// Instantiate the Upload Bean
Com. jspsmart. Upload. smartupload mysmartupload = new COM. jspsmart. Upload. smartupload ();
// Initialization
Mysmartupload. initialize (pagecontext );
// Sets the maximum upload value.
Mysmartupload. setmaxfilesize (500*1024*1024 );
// Upload a file
Mysmartupload. Upload ();
// Obtain all uploaded files cyclically
For (INT I = 0; I <mysmartupload. getfiles (). getcount (); I ++ ){
// Obtain the uploaded file
Com. jspsmart. Upload. File myfile = mysmartupload. getfiles (). GetFile (I );
If (! Myfile. ismissing ())
{
// Obtain the name of the uploaded file
String myfilename = myfile. getfilename ();
// Obtain a file name without a suffix
String suffix = myfilename. substring (0, myfilename. lastindexof ('.'));
// Get the suffix
String ext = mysmartupload. getfiles (). GetFile (0). getfileext ();
// Obtain the file size
Int filesize = myfile. getsize ();
// Save path
String AA = getservletcontext (). getrealpath ("/") + "JSP //";
String trace = AA + myfilename;
// Obtain other parameters
String explain = (string) mysmartupload. getrequest (). getparameter ("text ");
String send = (string) mysmartupload. getrequest (). getparameter ("send ");
// Save the file on the server
Myfile. saveas (trace, mysmartupload. save_physical );
// Save the uploaded file to the database.
// Read the object 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 ());
// Open the database
Resultset result = NULL;
String msql = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
// Write the file to the 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;
// Open the database
Resultset result = NULL;
String SQL = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
// Obtain data in the database
SQL = "select * From t_local_zhongzhuan ";
Result = dbaobj. executequery (SQL );
Result. Next ();

// Read data from the database 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 // read the stream
Inputstream instream = new fileinputstream ("C:/zsc104.swf ");
// Set the output format
Response. Reset ();
Response. setcontenttype ("bin ");
Response. addheader ("content-disposition", "attachment; filename =/" "+ filename + "/"");
// Cyclically retrieve the data in the stream
Byte [] B = new byte [100];
Int Len;
While (LEN = instream. Read (B)> 0)
Response. getoutputstream (). Write (B, 0, Len );
Instream. Close ();
%>
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.