Jsp file Upload simple instance sharing

Source: Internet
Author: User
Tags file upload

(I) form requirements

There are two requirements for the form for uploading files:

1. Apply post to method, that is, method = "post ".

2. Add attributes: enctype = "multipart/form-data"

The following is an example of a form used to upload files:

 

<Form method = "post" enctype = "multipart/form-data"
Action = "/jsp tutorial smartupload/upload. jsp">
<Input type = "file" name = "myfile">
<Input type = "submit">
</Form>
 


(Ii) upload example

1、upload page upload.html

This page provides a form that allows you to select the file to be uploaded and click "upload" to upload the file.

The page source code is as follows:

<! --
File name: upload.html
Author: Landscape Software Production Center Yu Yiqi (zhsoft88@sohu.com)
-->
<! Doctype html public "-// w3c // dtd html 4.01 transitional // en">
<Html>
<Head>
<Title> File upload </title>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
</Head>

<Body>
<P> </p>
<P align = "center"> select an upload file </p>
<Form method = "post" action = "jsp/do_upload.jsp"
Enctype = "multipart/form-data">
<Input type = "hidden" name = "test" value = "good">
<Table width = "75%" border = "1" align = "center">
<Tr>
<Td> <div align = "center"> 1,
<Input type = "file" name = "file1" size = "30">
</Div> </td>
</Tr>
<Tr>
<Td> <div align = "center"> 2,
<Input type = "file" name = "file2" size = "30">
</Div> </td>
</Tr>
<Tr>
<Td> <div align = "center"> 3,
<Input type = "file" name = "file3" size = "30">
</Div> </td>
</Tr>
<Tr>
<Td> <div align = "center"> 4,
<Input type = "file" name = "file4" size = "30">
</Div> </td>
</Tr>
<Tr>
<Td> <div align = "center">
<Input type = "submit" name = "submit" value = "upload it! ">
</Div> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>


 


2. Upload processing page do_upload.jsp

Upload files on this page. The page source code details the usage of the upload method, which will not be repeated here.

The page source code is as follows:

<% --
File name: do_upload.jsp
Author: Landscape Software Production Center Yu Yiqi (zhsoft88@sohu.com)
-- %>
<% @ Page contenttype = "text/html; charset = gb2312" language = "java"
Import = "java. util. *, com. jsps tutorial mart. upload. *" errorpage = "" %>
<Html>
<Head>
<Title> File upload processing page </title>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
</Head>

<Body>
<%
// Create a new smartupload object
Smartupload su = new smartupload ();
// Upload initialization
Su. initialize (pagecontext );
// Set Upload restrictions
// 1. Limit the maximum length of each uploaded file.
// Su. setmaxfilesize (10000 );
// 2. Restrict the total length of uploaded data.
// Su. settotalmaxfilesize (20000 );
// 3. Set the files that can be uploaded (with the extension limit). Only the doc and txt files are allowed.
// Su. setallowedfileslist ("doc, txt ");
// 4. Set files that are not allowed to be uploaded (by using the extension limit). Upload with exe, bat, is prohibited,
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 ");
Out. println (count + "files uploaded successfully! <Br> ");

// Obtain the parameter value using the request object
Out. println ("test =" + su. getrequest (). getparameter ("test ")
+ "<Br> ");

// Extract the uploaded file information one by one and save the file.
For (int I = 0; I <su. getfiles (). getcount (); I ++)
{
Com. jspsmart. upload. file = su. getfiles (). getfile (I );

// Continue if the object does not exist
If (file. ismissing () continue;

// Display the current file information
Out. println ("<table border = 1> ");
Out. println ("<tr> <td> form item name (fieldname) </td> <td>"
+ File. getfieldname () + "</td> </tr> ");
Out. println ("<tr> <td> File size </td> <td>" +
File. getsize () + "</td> </tr> ");
Out. println ("<tr> <td> file name (filename) </td> <td>"
+ File. getfilename () + "</td> </tr> ");
Out. println ("<tr> <td> File extension (fileext) </td> <td>"
+ File. getfileext () + "</td> </tr> ");
Out. println ("<tr> <td> File full name (filepathname) </td> <td>"
+ File. getfilepathname () + "</td> </tr> ");
Out. println ("</table> <br> ");

// Save the file
// File. saveas ("/upload/" + myfile. getfilename ());
// Save it to the directory where the root directory of the web application is the root directory of the file.
// File. saveas ("/upload/" + myfile. getfilename (),
Su. save_virtual );
// Save the file to the root directory of the operating system
// File. saveas ("c: temp" + myfile. getfilename (),
Su. save_physical );

}
%>
</Body>
</Html>

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.