Smartupload implementation of File upload, download

Source: Internet
Author: User

Package com.yc.util;


Import java.io.IOException;
Import java.sql.SQLException;
Import java.util.Collection;
Import Java.util.Date;
Import java.util.Enumeration;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Random;


Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletResponse;
Import Javax.servlet.jsp.PageContext;


Import Com.jspsmart.upload.File;
Import Com.jspsmart.upload.Files;
Import Com.jspsmart.upload.Request;
Import Com.jspsmart.upload.SmartUpload;
Import com.jspsmart.upload.SmartUploadException;


public class Uploadutil {
private static final String PATH = "Images"; folder where pictures are saved
Private static final String allowed = "Gif,jpg,png,jpeg"; Types of files that can be uploaded
private static final String DENIED = "exe,bat,jsp,html,com"; File types that cannot be uploaded
private static final int totalmaxsize = 20 * 1024 * 1024; The maximum upload size
private static final int singlefilesize = 1024 * 1024; Up to a single file in small


/**
* Used to read the string parameters and file parameters passed from the page context to request requests.
*
* @param context
*: Page context
* @return Map key value name-> Zhang San
*/
@SuppressWarnings ({"Unchecked", "Rawtypes"})
Public map<string,string> Upload (PageContext context) {
Smartupload su = new Smartupload ();
File F=null; Temporary variables are used to store each uploaded file, not io, but smartupload
Start taking out the page all the character parameters that came
map<string, string> params = new hashmap<string, string> ();


Initialization
try {
Su.initialize (context);
Setting parameters
Su.setallowedfileslist (allowed);//set allowed files to be uploaded (limited by extension)
Su.setdeniedfileslist (DENIED); Set files that are blocked from uploading (restricted by extension)
Su.setcharset ("Utf-8");
Su.settotalmaxfilesize (totalmaxsize); Limit the length of the total upload data.
Su.setmaxfilesize (singlefilesize);


Su.upload ();//start receiving uploads
int count = Su.save ("/upload"); Save all uploaded files to the specified directory




Download a new
Smartupload su = new Smartupload ();


Initialization
Su.initialize (PageContext);


Set Contentdispositio to NULL to prevent the browser from automatically opening files


Make sure to download the file after clicking on the link. If not set, the downloaded file name extension is doc,
The browser will automatically open it with Word. When the extension is PDF, the browser opens with Acrobat.
Su.setcontentdisposition (NULL);
Download documents
Su.downloadfile ("/upload/test.doc");


Remove the package request from the SU
Request Request = Su.getrequest (); Note This reqeust please 滶 object is provided by Smartupload


Enumeration Et=request.getparameternames (); Get all form element information in a request
String str;


while (Et.hasmoreelements ()) {
Str=string.valueof (Et.nextelement ());
Params.put (Str,request.getparameter (str));
}
Take out the uploaded file stream
if (su.getfiles () = null && su.getfiles (). GetCount () > 0) {
Files fs = Su.getfiles ();
collection<file> col = fs.getcollection ();
String fname;
String picpath= "";
for (File F:col) {
if (!f.ismissing ()) {//Determine if the file is missing during upload
The file name may be duplicated, so the original file name cannot be used, and a new filename must be rebuilt.
fname = path+ "/" +new Date (). GetTime () + "" + New Random (). Nextint (10000) + "." +f.getfileext (); Get the suffix name of the original file
System.out.println ("file will be saved to:" + fname);


Save
F.saveas (fname, smartupload.save_virtual);
Picpath+=fname;
}
}
Picpath+=picpath.substring (0,picpath.lastindexof (",")); Remove the last comma.
Params.put ("Picpath", Picpath); Remove the last saved file name and save it to the params
}

} catch (Servletexception e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} catch (IOException e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} catch (SQLException e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} catch (Smartuploadexception e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
}
return params;
}

/**
* File Download
* @param filePath: The file path to download
* @param response: Response
* @param context: File contexts
*/
public void DownLoadFile (String filePath, httpservletresponse response,pagecontext context) {
try {
Smartupload su=new smartupload ();
Su.initialize (context);
Set contentdisposition to NULL to prevent the browser from automatically opening files
Make sure to download the file after clicking Connect. If not set, the downloaded file name extension is doc
The browser will automatically open with Word. When the extension is PDF, the browser opens with Acrobat
Su.setcontentdisposition (NULL);
Download file


Su.downloadfile (FilePath);
} catch (Smartuploadexception e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} catch (Servletexception e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} catch (IOException e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
} finally{
try {
Response.getoutputstream (). Close ();
} catch (IOException e) {
LogUtil.log.error (E.tostring ());
throw new RuntimeException (e);
}
}
}
}

Smartupload implementation of File upload, download

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.