NOTES: JDBC File upload & download

Source: Internet
Author: User
Tags uuid

First, upload and download the implementation principle

File upload and download the implementation principle,

Upload principle:

1, the client browser through the file browse box, select the file content to upload (including file path and file content).

2, the client browser through the upload button, the local file upload to the browser.

3, the server receives the local file content through the program, and saves it in the server's disk.

Download implementation process:

1. The client browser downloads the file saved by the server to the local disk by clicking the Download button.

2, the server side through the program to respond to the server files to the client.

Second, the file upload:
* Analysis Implementation steps:
* The client browser selects the files to be uploaded locally via the file domain.
* Click on the "Upload" button to send the uploaded file to the server side.
* The server side receives the upload file sent by the client.
* Define a path to save the uploaded file.
* Save the uploaded file in this path.
* Will upload the results after the response to the client browser.
Client
* Page elements:
* Define a form for sending requests to the server side.
* The form has a file field for selecting the files to be uploaded locally.
* There is a submit button in the form for submitting the request.
Problem
* When submitting a form, simply upload the file name of the file field selection. (Do not submit the uploaded file)
* Set enctype= "Multipart/form-data" for the form
Note
* The file domain must specify the Name property value, otherwise the file data that needs to be uploaded is not uploaded to the server side.
* The request type of the form that completes the file upload function must be the post mode.
* The Enctype property value of the form that completes the file upload function is set to "Multipart/form-data",
The purpose of this value is to add the file data that needs to be uploaded to the HTTP request body and to describe the uploaded file using the MIME protocol.
* Server side:
* Third-party tools: FileUpload
* Using FileUpload components:
* Commons-fileupload-1.3.1.jar
* Commons-io-2.2.jar
* Import the jar package into the Web project.

* Attention to things:
* The directory where the uploaded files are saved is placed in the root directory of the Web application. (unsafe)
* Description of the problem:
* After saving the uploaded file, it can be accessed through the browser.
* Example: If the uploaded file is a JSP page, contains Java code (implementation of 10 seconds automatic shutdown).
* Solution:
* The directory where the uploaded files will be saved is set in a location that cannot be accessed by the browser.
* The directory where the uploaded files will be saved can be placed in the "Web-inf" directory.
* Processing the uploaded file name:
* Description of the problem:
* Some browsers (IE 6), the uploaded file name is not a simple file name, but the true path of the file.
* Solution:
* Determine whether the currently acquired file name is the real filename or the true path of the uploaded file.
* If it is a real path, remove all paths before the file name.
The file name may be an absolute path for cutting
int index = filename.lastindexof ("\ \");
Gets the content after "\" (the real file name)
if (index >= 0) {
FileName = filename.substring (index + 1);
}
* Handling of uploading files in Chinese garbled problem:
* Description of the problem:
* The name of the uploaded file has a problem with Chinese garbled characters.
* The normal field value in the Upload form has a problem with Chinese garbled characters.
* Solution:
* Upload file name:
* Request.setcharacterencoding ("Utf-8");
* Servletfileupload.setheaderencdoing ("UTF-8");
* Normal field value:
* FILEITEM.GETSTRING (encoded format)-Encoding format: Indicates why the current text content is encoded.
* Uploading file names with the same name problem:
* Description of the problem:
* Before and after uploading two files of the same name, but the content is different.
* After uploading the file will overwrite the previously uploaded files.
* Solution:
* Generate unique ID:UUID.randomUUID (). toString () + "_" +filename;
* A directory cannot hold multiple files:
* Description of the problem:
* If there are too many uploads in a directory, it is inconvenient to read.
* Solution:
* Upload files into multiple directory stores:
* Sorted by Date: 2015-03-31, 2015-04-01, etc.
* Sub-directories by MIME type: text format, pictures, videos, etc.
* According to the user ID sub-directory: User1, user2, etc.
* Generate a multi-level directory with hashcode that generate random numbers using UUID:
* Limit Single File upload size: Upload.setfilesizemax (1024*1024*3);
* Limit the total size of uploaded files: Upload.setsizemax (1024*1024*10);
* File cache size with temp directory
* Set TEMP directory
* The default temp directory is System.getproperty ("Java.io.tmpdir").
* Diskfileitemfactory.setrepository (New File (Getservletcontext (). Getrealpath (Temporary directory relative path));
* After the file upload is complete, call Fileitem's Delete () method to delete the temporary files in the temp directory. (Release resources)
* Set Cache Size
* If the cache size is not set manually, the default is 10KB.
* Diskfileitemfactory.setsizethreshold (number of cache bytes);
* Manually set the cache size to 10M, what does that mean?
* If the upload file is less than 10M, there is a server-side content.
* If the upload file is larger than 10M, there is a temporary directory on the server side.
* Note: If you set the cache size too large, the performance on the server side is affected.
* Calculate File Upload Progress control:
* To calculate the upload progress of the relevant information:
* Used time: Current time-start time
* Speed: Uploaded size/used time
* Remaining size: Total size – already uploaded size
* Remaining time: Remaining size/speed
* How to implement monitoring upload progress:
* Implemented using the Setprogresslistener () method provided by Servletfileupload.
* Implement multi-File upload page:
Classification
* A form contains multiple file fields.
* At the same time select multiple files to upload, submit the upload together. (Problem: Either succeed together or fail together)
* A file domain one upload, multiple upload files are independent. (Ajax Technology: Asynchronous interaction)
* Multi-File Upload page to achieve dynamic.
* File Download:
Problem
* The download page provided, use the <a> link to point to the resource file to be downloaded.
* If the browser supports the format, the display is developed directly in the browser.
* If the browser does not support the format, provide download options (available for download).
* Download Required Now: Regardless of the format of the download file, whether the browser supports it, you need the download option.
* The file download function must be done through the server-side servlet.
* How to use File download:
* Create a file download page that will request the server-side servlet to process when the user clicks on the download.
* Steps of the server-side servlet implementation:
* Gets the name of the download file submitted by the client.
* Gets the directory structure that stores the downloaded files.
* Read the corresponding download file content via the input stream.
* Gets the OutputStream output stream of the response object.
* will read the input stream, output to the output stream of the response object, and respond to the client browser download.
Problem
* Sets the MIME type of the current download file.
Response.setcontenttype (Getservletcontext (). GetMimeType (filename));
* Setting the notification browser The current download must provide the download option instead of opening it directly.
Response.setheader ("Content-disposition", "attachment;filename=" +filename);
* Download Chinese garbled problem:
* Resolve the Get mode request download file name in Chinese garbled:
New String (Filename.getbytes ("iso-8859-1"), "Utf-8");
* When downloading the corresponding file, the download file name is garbled in Chinese:
String useragent = Request.getheader ("user-agent");
if (Useragent.contains ("MSIE")) {
Indicates that it is currently IE browser
filename = urlencoder.encode (filename, "utf-8");
filename = filename.replace ("+", "");
}else{
Base64encoder Base64encoder = new Base64encoder ();
filename = "=?utf-8?" B? "+ Base64encoder.encode (filename.getbytes (" utf-8 ") +"? = ";
}

  

NOTES: JDBC 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.