5. Requirements for file Download:
1). Provide a "Download resource" hyperlink on the success.jsp page after the file upload is successful
2). Click on the "Download resource" hyperlink to send the request to the Servlet, read the database, and display the resource information available for download on the page.
Filename:11 Silicon Valley _javaweb_ listener. pptx
Desc:aa
Download
Filename:12. Uploading and downloading of _javaweb_ files in Silicon Valley. pptx
Desc:bb
Download
3). Click Download to complete the download of the corresponding file.
4. Download the file:
1). Steps:
I. Setting the ContentType response header: What is the type of set response? Notifies the browser that it is a downloaded file
Response.setcontenttype ("Application/x-msdownload");
Ii. Setting the Content-disposition response header: Notifies the browser that there is no longer a browser to process (or open) the file to be downloaded, which is done manually by the user
Response.setheader ("Content-disposition", "attachment;filename=abc.txt");
Iii. specific files: You can call Response.getoutputstream in the form of an IO stream sent to the client.
OutputStream out = Response.getoutputstream ();
String pptfilename = "C:\\users\\think pad\\desktop\\__ is in class __\\11." _javaweb_ Listener in Silicon Valley. pptx ";
InputStream in = new FileInputStream (pptfilename);
byte [] buffer = new byte[1024];
int len = 0;
while (len = in.read (buffer))! =-1) {
Out.write (buffer, 0, Len);
}
In.close ();
3. How do I modify the source code of a gadget or frame?
1). Principle: Can not modify without modification.
2). Method of modification:
> Modify the source code to replace the corresponding class file in the jar package.
> Create the same package locally, and class, and modify it in this class.
2. Use the FileUpload component to complete the upload application of the file
1). Requirements:
I. Upload
> Use jQuery on the upload.jsp page to "Add an attachment," "Delete attachment." But you need to keep at least one.
> validates the file's extension and file size. The following rules are configurable. Rather than writing dead in the program.
>> file extension must be. pptx, docx, doc
>> the size of each file cannot exceed 1 M
>> The total file size cannot exceed 5 M.
> If validation fails, an error message is displayed on the upload.jsp page:
>> If a file does not meet the requirements: The xxx file extension is not valid or the xxx file size exceeds 1 M
>> The total file size cannot exceed 5 M.
> Upload the file if the validation is passed
>> file upload and give a name that cannot be duplicated with other files, but the extension does not change
>> adds a record to the corresponding data table.
ID file_name file_path File_desc
II. Download
Problem: How to clear the files that upload files Temp folder.
1. When uploading a file, the form needs to be prepared:
1). Request mode is post: <form action= "Uploadservlet" method= "POST" ... >
2). form fields using file: <input type= "file" name= "file"/>
3). Request encoding using Multipart/form-data: <form action= "Uploadservlet" method= "post" enctype= "Multipart/form-data" >
<form action= "Uploadservlet" method= "post" enctype= "Multipart/form-data" >
File: <input type= "file" name= "file"/>
<input type= "Submit" value= "Submit"/>
</form>
4). About Enctype:
> application/x-www-form-urlencoded: The default value of the form Enctype property. This encoding scheme uses a limited set of characters, and when non-letters and numbers are used,
Must be replaced with "%hh" (H for hexadecimal digits). For large-capacity binary data or text that contains non-ASCII characters, this encoding does not meet the requirements.
> Multipart/form-data:form Sets the Enctype= "Multipart/form-data" property to indicate that the form transmits data in binary
2. Service side:
1). You can no longer use Request.getparameter () and other methods to obtain request information. Not available because the requested encoding has been changed to Multipart/form-data to
Binary way to submit the request information.
2). You can use the input stream to get it. However, this is not recommended.
3). Use the Commons-fileupload component specifically to upload the file.
I. Setting up the environment: join
Commons-fileupload-1.2.1.jar
Commons-io-2.0.jar
Ii. Basic Ideas:
> Commons-fileupload can parse the request and get a List of Fileitem objects
> commons-fileupload resolves all request information to a Fileitem object, whether it is a generic text field or a file field.
> can call Fileitem's Isformfield () method to determine whether it is a form field or not a table fields (then a file field)
> to get further information
if (Item.isformfield ()) {
String name = Item.getfieldname ();
String value = item.getstring ();
...
}
if (!item.isformfield ()) {
String fieldName = Item.getfieldname ();
String fileName = Item.getname ();
String ContentType = Item.getcontenttype ();
Boolean isinmemory = Item.isinmemory ();
Long sizeinbytes = Item.getsize ();
InputStream Uploadedstream = Item.getinputstream ();
...
Uploadedstream.close ();
}
Iii. how to get the List<fileitem> object.
> A simple way
Create a factory for disk-based file items
Fileitemfactory factory = new Diskfileitemfactory ();
Create a new file upload handler
Servletfileupload upload = new Servletfileupload (factory);
Parse the request
List/* Fileitem */items = upload.parserequest (request);
> Complex way: You can add some restrictions and other attributes to file uploads
Create a factory for disk-based file items
Diskfileitemfactory factory = new Diskfileitemfactory ();
Set the maximum size of the uploaded file in memory and write the file to a temporary folder if it is exceeded. In units of byte
Factory.setsizethreshold (yourmaxmemorysize);
Set the Temp folder
Factory.setrepository (yourtempdirectory);
Create a new file upload handler
Servletfileupload upload = new Servletfileupload (factory);
Sets the total size of the uploaded file. You can also set the size of a single file.
Upload.setsizemax (yourmaxrequestsize);
Parse the request
List/* Fileitem */items = upload.parserequest (request);
-----------------------------------------------
Question 1: If it is a multi-select, how to get the corresponding string array. Each one corresponds to a Fileitem object.
<input type= "checkbox" Name= "Interesting" value= "Reading"/>reading
<input type= "checkbox" Name= "interesting" value= "party"/>party
<input type= "checkbox" Name= "Interesting" value= "Sports"/>sports
<input type= "checkbox" Name= "interesting" value= "shopping"/>shopping
Question 2. How to Empty the Temp folder: The way to delete it manually.
Study Notes (11) File upload download