Jspsmartupload Implementing uploads

Source: Internet
Author: User

2. Save
Function: Saves all uploaded files to the specified directory and returns the number of files saved.
Prototype: public int Save (String destpathname)
and public int Save (String destpathname,int option)
Where Destpathname saves the directory for the file, option is the save options, it has three values, respectively, 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 to the directory that is the root of the file at the operating system root, Save_virtual instructs the component to save the file to the directory that is the root of the Web application root directory. Save_auto, however, indicates that the component is automatically selected.

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 parameter before you can upload or download files .

<%mysmartupload.initialize (PageContext); Perform 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 ());}} %>

here, the file is uploaded to the upload folder in the root directory through the Save () method.


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)
Where Destfilepathname is the saved file name, Optionsaveas is the saved option, which has three values, respectively, Saveas_physical,saveas_virtual,saveas_auto.Saveas_physical indicates that the file is saved as the root directory of the operating system, saveas_virtual indicates that the file root is saved as the root directory of the Web application, saveas_auto means that the component decides that when a directory exists for the file to be stored in the root directory of the Web application, it chooses Saveas_virtual, or saveas_physical is selected.
For example, after the SaveAs ("/upload/sample.zip", saveas_physical) executes, if the Web server is installed on drive C, the saved file name is actually c:\upload\sample.zip. While the SAVEAS ("/upload/sample.zip", saveas_virtual) executes if the Web application's root directory is webapps/jspsmartupload, the saved file name is actually webapps/ Jspsmartupload/upload/sample.zip. SaveAs ("/upload/sample.zip", Saveas_auto) executes when the upload directory exists under the Web application root, with the effect of the SaveAs ("/upload/sample.zip", Saveas_ VIRTUAL), otherwise with SAVEAS ("/upload/sample.zip", saveas_physical).
Suggestions:for the development of Web programs, 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, where Saveas_virtual is stored in the Upload folder in the Web project.

The following code uses saveas_physical, the same functionality as the above code, where pagecontext.getservletcontext (). Getrealpath ("/") to get the Webapps/project path.


<%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 ());}} %>



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.