2. Save
Function: Saves all uploaded files to the specified folder and returns the number of files saved.
Prototype: public int Save (String destpathname)
and public int Save (String destpathname,int option)
In the Destpathname folder for the file, option is the Save option, which has three values, each of which is save_physical,save_virtual and Save_auto.
(similar to the value of the option for the SaveAs method of the file class) Save_physical instructs the component to save the file under the folder that is the root folder of the operating system root folder. Save_virtual instructs the component to save the file to a folder with the root folder of the Web application as the root folder of the file, while Save_auto indicates that the component is actively selected by its own.
Note: the Save (destpathname) function is equivalent to save (Destpathname,save_auto).
<form method= "POST" action= "uploadfile.jsp" enctype= "Multipart/form-data" ><input type= "file" name= "file" Size= "> </form>
here enctype= "Multipart/form-data" is the MIME encoding of the form. This number of participants is enough to upload or download files .
<%mysmartupload.initialize (PageContext); Run initialization operation Mysmartupload.upload (); Upload file Dataint size = 1024x768 * 1024x768 * 1024;IF (Mysmartupload.getfiles (). GetSize () > Size) {out.println ("the Files Has to be < 1024MB! ");} else {try {mysmartupload.save ("/upload"); Out.print ("Upload file successfully!");} catch (Exception e) {out.print (e.tostring ());}} %>
This is done through the Save () method. Upload the file to the upload folder in the root folder.
1, the role of the SaveAs: Save the file name.
Prototype:
public void SaveAs (java.lang.String destfilepathname)
Or
public void SaveAs (java.lang.String destfilepathname, int optionsaveas)
The Destfilepathname is the name of the saved file. Optionsaveas is a saved option with three values. Each is Saveas_physical,saveas_virtual,saveas_auto.
saveas_physical indicates that the file root folder is saved as the root folder of the operating system. Saveas_virtual indicates that the file root folder is saved as the root folder of the Web application . Saveas_auto indicates that the component is determined. When the root folder of the Web application exists, it chooses saveas_virtual, or saveas_physical is selected.
Like what. SaveAs ("/upload/sample.zip", saveas_physical) after running if webserver installed on C drive. Then the saved file name is actually c:\upload\sample.zip. While the SAVEAS ("/upload/sample.zip", saveas_virtual) runs after the Web application's root folder is Webapps/jspsmartupload, the saved file name is actually webapps/ Jspsmartupload/upload/sample.zip.
SaveAs ("/upload/sample.zip", Saveas_auto) runs when the upload folder exists under the Web application root folder. The effect is the same as the SAVEAS ("/upload/sample.zip", saveas_virtual). Otherwise with SAVEAS ("/upload/sample.zip", saveas_physical).
Recommendation: for Web program development, it is best to use saveas_virtual for porting.
Saveas_physical is an absolute path, and saveas_virtual is a relative path (equivalent to the preceding plus tomcat/webapps/yourproject/).
<%mysmartupload.initialize (PageContext); Initiatemysmartupload.upload (); Upload file Dataint size = 1024x768 * 1024x768 * 1024;IF (Mysmartupload.getfiles (). GetSize () > size) {//control the size of T He fileout.println ("The files have been < 1024MB!");} else {try {for (int i = 0; i < Mysmartupload.getfiles (). GetCount (); i++) {//iterating the filesfile file = Mysmartuploa D.getfiles (). GetFile (i); if (file.ismissing ()) continue; String virtualpath = "/upload/";//tomcat/webapps/yourproject/uploadfile.saveas (VirtualPath + file.getFileName (), mysmartupload.save_virtual);} Out.print ("Upload file successfully!");} catch (Exception e) {out.print (e.tostring ());}} %>
The code above uses the SaveAs method, in which saveas_virtual is stored in the upload directory in the Web project.
The following code uses saveas_physical, and the same functionality as the above code, where pagecontext.getservletcontext (). Getrealpath ("/") to get the path to Webapps/project.
<%mysmartupload.initialize (PageContext); Initiatemysmartupload.upload (); Upload file Dataint size = 1024x768 * 1024x768 * 1024;IF (Mysmartupload.getfiles (). GetSize () > size) {//control the size of T He fileout.println ("The files have been < 1024MB!");} else {try {for (int i = 0; i < Mysmartupload.getfiles (). GetCount (); i++) {//iterating the filesfile file = Mysmartuploa D.getfiles (). GetFile (i); if (file.ismissing ()) continue; String PhysicalPath = Pagecontext.getservletcontext ()//tomcat/webapps/yourproject/upload.getrealpath ("/") + "/ Upload/"; File.saveas (PhysicalPath + file.getfilename (), mysmartupload.save_physical);} Out.print ("Upload file successfully! ");} catch (Exception e) {out.print (e.tostring ());}} %>
Jspsmartupload Implementing uploads