servlet upload Download

Source: Internet
Author: User

Here is a JSP page, the form has 4 file input text box, you can upload 4 files:
<form action= "File?file=uploadbyjs" method= "post" enctype= "Multipart/form-data" >
<input type= "File" Name=file1 "size=" ><Br>
<input type= "File" Name=file2 "size=" ><Br>
<input type= "File" Name=file3 "size=" ><Br>
<input type= "File" Name=file4 "size=" ><Br>
<input type= "Submit" value= "Upload" >
</form>
Of course you can set up to upload more files at the same time ...
Servlet handlers:
1String Path = "D:/work space/jsp workspace/jsp_servlet_upandload/file/upload";
2//Create a new Jsmartupload object
3 Smartupload smartupload = new Smartupload ();
4//Upload initialization
5 smartupload.initialize (This.getservletconfig (), request,response);
6 try {
7//Set upload limit
8//Limit the maximum length of each uploaded file; set Max to 1024*1024*20
9 smartupload.setmaxfilesize (1024*1024*10);
10//Limit the length of total uploaded data
Smartupload.settotalmaxfilesize (1024*1024*20);
12//Limit the types of files allowed to upload, allow doc, txt, bat files
Smartupload.setallowedfileslist ("Doc,txt,bat");
14//Limit file types that are not allowed to upload, disallow EXE, JSP, and files without extensions
Smartupload.setdeniedfileslist ("exe,jsp,,");
16//Upload files
Smartupload.upload ();
18//Save the file to the specified directory
Smartupload.save (path);
catch (SQLException e) {
E.printstacktrace ();
\ catch (Smartuploadexception e) {
E.printstacktrace ();
24}
25
26//Extract the file information, and output the information of the uploaded file
(int i = 0; i < Smartupload.getfiles (). GetCount (); i++) {
Com.jspsmart.upload.File myFile =smartupload.getfiles (). GetFile (i);
29//If file in file Form option does not select file then continue
if (myfile.ismissing ())
Continue;
32//Display information for the current file
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("<table border= ' 1 ' >");
Out.println ("<tr><td> form Options </td><td>" +myfile.getfieldname () + "</td></tr>");
PNS out.println ("<tr><td> file length:</td><td>" +myfile.getsize () + "</td></tr>");
OUT.PRINTLN ("<tr><td> filename </td><td>" +myfile.getfilename () + "</td></tr>");
Out.println ("<tr><td> file extension </td><td>" +myfile.getfileext () + "</td></tr>");
Out.println ("<tr><td> file name </td><td>" +myfile.getfilepathname () + "</td></tr> ");
Out.println ("</table><br>");
42}
The program uses the Smartuploa object directly to implement file uploads. After the object is requested, it must be initialized: Smartupload.initialize (This.getservletconfig (), request,response);

Using the Jspsmartupload component for file download can be very simple:
JSP page:

<a href= "${pagecontext.request.contextpath}/file1?file=downbyjsmart&name=user.txt" > Download user</a>
1//Get download file name
2 String fileName = Request.getparameter ("name");
3//Create a new Smartupload object
4 Smartupload smartupload = new Smartupload ();
5//initialization
6 smartupload.initialize (This.getservletconfig (), request, response);
7//Set contentdisposition to NULL to prevent the browser from automatically opening files
8//Guarantee after clicking the link is the download file.
9 smartupload.setcontentdisposition (NULL);
10//Download file
try {
Smartupload.downloadfile ("D:/work space/jsp workspace/jsp_servlet_upandload/file/upload/" +fileName);
(Smartuploadexception e) {
E.printstacktrace ();
15}

Servlet 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.