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:
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 ();
Class
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
%>
Again download, download is divided into two cases 1. Download 2 directly from the database. Download from server
First of all, the case of downloading directly from the database: the input stream is read from the database and then transferred to the 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 Database
ResultSet Result=null;
String Sql=null;
PreparedStatement Prestmt=null;
dbstep.idbmanager2000 dbaobj=new dbstep.idbmanager2000 ();
Dbaobj.openconnection ();
Get the data in the database
Sql= "SELECT * from T_local_zhongzhuan";
Result=dbaobj.executequery (SQL);
Result.next ();
Read data from a database into a 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);
}
%>
Again, download from the server:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "java.io.*"%>
<%
String fileName = "zsc104.swf". toString ();
f//read into the stream
InputStream instream=new FileInputStream ("c:/zsc104.swf");
Format the output
Response.reset ();
Response.setcontenttype ("bin");
Response.AddHeader ("Content-disposition", "attachment; filename=/" "+ filename +"/");
Loop out 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 ();
%>
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.