JSP to upload file support is not as good as the support in PHP, directly into the function, and not like the ASP in order to achieve through the component. The JSP can be implemented by JavaBean. But we do not have to write an uploaded bean on our own, and Smartupload is one of the many technologies that have been formed on the Internet.
But Smartupload is to read the file first to the server's memory, so upload too large files (more than 100 trillion) there may be problems, but also a flaw in the bar:
First of all, the submission of the page, the Smartupload component requires a byte stream to submit <form action= "upload.jsp" Enctype=multipart/form-data method=post>. Here's an example upload.htm:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<!--saved from Url= (0057) http://localhost:8080/jspsmartfile/jsp/uploadTemplate.jsp-->
<HTML><HEAD>
<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> File
: </font> <input size=60 type=file name= "file" ></TD></TR>
<TR>
<TR>
<td><font color= #000000 Face=helv,helvetica size=1> File
: </font> <input size=60 type=file name= "File1" ></TD></TR>
<TR>
<td><font color= #000000 Face=helv,helvetica size=1> File
: </font> <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>
Take a look at the received page, we upload the file to the server and then directly put it into the database: upload.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "com.jspsmart.upload.*"%>
<%@ page import= "dbstep.idbmanager2000.*"%>
<%
Instantiating an upload bean
Com.jspsmart.upload.SmartUpload mysmartupload=new com.jspsmart.upload.SmartUpload ();
Initialization of
Mysmartupload.initialize (PageContext);
Set the maximum upload value
Mysmartupload.setmaxfilesize (500 * 1024*1024);
Uploading files
Mysmartupload.upload ();
Loop to get all uploaded files
for (int i=0;i<mysmartupload.getfiles (). GetCount (); i++) {
Get the uploaded file
Com.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (i);
if (!myfile.ismissing ())
{
Gets the filename of the uploaded file
String Myfilename=myfile.getfilename ();
Get filename with no suffix
String suffix=myfilename.substring (0,myfilename.lastindexof ('. '));
Get suffix Name
String ext= mysmartupload.getfiles (). GetFile (0). Getfileext ();
To get the size of a file
int filesize=myfile.getsize ();
Save path
String Aa=getservletcontext (). Getrealpath ("/") + "JSP";
String Trace=aa+myfilename;
Get another parameter
String explain= (String) mysmartupload.getrequest (). GetParameter ("text");
String send= (String) mysmartupload.getrequest (). GetParameter ("send");
Save the file on the server side
Myfile.saveas (trace,mysmartupload.save_physical);
The following is to save the uploaded file to the database
Read the file to the stream
Java.io.File File = new Java.io.File (trace);
Java.io.FileInputStream fis = new Java.io.FileInputStream (file);
Out.println (File.length ());
Open Database
ResultSet Result=null;
String Msql=null;
PreparedStatement Prestmt=null;
dbstep.idbmanager2000 dbaobj=new dbstep.idbmanager2000 ();
Dbaobj.openconnection ();
Write a file to a database
Msql= "INSERT into marklist (markname,password,marksize,markdate,markbody) VALUES (?,?,?,?,?)";
Prestmt =dbaobj.conn.preparestatement (mSQL);
Prestmt.setstring (1, "Aaa1");
Prestmt.setstring (2, "0000");
Prestmt.setint (3, fileSize);
Prestmt.setstring (4, Dbaobj.getdatetime ());
Prestmt.setbinarystream (5,fis, (int) file.length ());
DbaObj.Conn.setAutoCommit (TRUE);
Prestmt.executeupdate ();
DbaObj.Conn.commit ();
Out.println (("Upload succeeded!!! "). toString ());
}
Else
{Out.println (("Upload failed!!! "). toString ());
}//with the previous if corresponding
%>