Jsp upload/download + SmartUpload plugin upload

Source: Internet
Author: User

You need to download the jspSmartUpload. jar package by yourself before using it.

Here we find a jar package that supports Chinese characters, as shown below: http://www.bkjia.com/uploadfile/2012/0426/20120426093645364.rar
 

Copy the jar in the compressed package to the lib of the project.

Main Methods

Upload

// Create a new SmartUpload object
SmartUpload su = new SmartUpload ();
// Upload Initialization
Su. initialize (pageContext );
// Limit the maximum length of each uploaded file.
Su. setMaxFileSize (10000 );
// Limit the total length of uploaded data.
Su. setTotalMaxFileSize (20000 );
// Set the files that can be uploaded (with the extension limit). Only the doc and txt files are allowed.
Su. setAllowedFilesList ("doc, txt ");
// Sets the file that is not allowed to be uploaded (by using the extension limit). It is forbidden to upload files with exe, bat,
// Jsp, htm, html extension files and files without extension.
Su. setDeniedFilesList ("exe, bat, jsp, htm, html ,,");
// Upload a file
Su. upload ();
// Save all uploaded files to the specified directory
Int count = su. save ("/upload ");


Download

// Create a new SmartUpload object
SmartUpload su = new SmartUpload ();
// Initialization
Su. initialize (pageContext );
// Set contentDisposition to null to prevent the browser from automatically opening the file,
// Ensure that the object is downloaded after the link is clicked. If this parameter is not set, the downloaded file name extension is
// When the doc is used, the browser automatically opens it with word. When the extension is pdf,
// The browser will be opened with acrobat.
Su. setContentDisposition (null );
// Download an object
Su. downloadFile ("/upload/test.doc ");

**************************************** **************************************** **************************************

 

Application instance:

 

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. io. *, java. awt. image, java. awt. image. *, com.sun.image.codec.jpeg. *, java. SQL. *, com. jspsmart. upload. *, java. util. * "%>
<%
SmartUpload mySmartUpload = new SmartUpload ();
Long file_size_max = 4000000;
String fileName2 = "", ext = "", testvar = "";
String url = "upload/"; // ensure that this directory exists in the root directory (that is, you need to create the corresponding folder yourself)
// Initialization
MySmartUpload. initialize (pageContext );
// Only files of this type can be uploaded
Try {
MySmartUpload. setAllowedFilesList ("jpg, gif"); // you can modify the file format as needed.
// Upload a file
MySmartUpload. upload ();
} Catch (Exception e ){
%>
<SCRIPT language = javascript>
Alert ("Upload only. jpg and. GIF image files allowed ");
Registry.location='upload.htm ';
</Script>
<%
}
Try {

Com. jspsmart. upload. File myFile = mySmartUpload. getFiles (). getFile (0 );
If (myFile. isMissing () {%>
<SCRIPT language = javascript>
Alert ("select the file to upload first ");
Registry.location='upload.htm ';
</Script>
<%}
Else {
// String myFileName = myFile. getFileName (); // get the name of the uploaded file
Ext = myFile. getFileExt (); // get the suffix
Int file_size = myFile. getSize (); // get the file size
String saveurl = "";
If (file_size <file_size_max ){
// Change the file name to obtain the millisecond value of the current upload time
Calendar calendar = Calendar. getInstance ();
String filename = String. valueOf (calendar. getTimeInMillis ());
Saveurl = application. getRealPath ("/") + url;
Saveurl + = filename + "." + ext; // save path
MyFile. saveAs (saveurl, SmartUpload. SAVE_PHYSICAL );
Out. print (saveurl );

String ret = "parent. HtmlEdit. focus ();";
Ret + = "var range = parent.HtmlEdit.doc ument. selection. createRange ();";
Ret + = "range. pasteHTML (' ');";
Ret + = "alert ('upload successful! ');";
Ret + = "too many location='upload.htm ';";
Out. print ("<script language = javascript>" + ret + "</script> ");

}
}
} Catch (Exception e ){
Out. print (e. toString ());
}
%>

 


Upload.htm

<Html>
<Head>
<Title> select an uploaded image </title>
</Head>
<Body>
<Table border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr>
<Td height = "45" align = "center" valign = "middle"> <form action = "uploadimage. jsp "method =" post "enctype =" multipart/form-data "name =" form1 ">
<Input type = "file" name = "file">
<Input type = "submit" name = "Submit" value = "Upload">
</Form> </td>
</Tr>
</Table>
</Body>
</Html>


From play to java

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.