Using the servlet to download the file, the principle is very simple, as long as the file's input stream (or the corresponding byte), then write the output stream. There are several details of the details:
1. MIME type setting:
The web browser uses the MIME type to identify non-HTML documents and determine how data within the document.
For example, the MIME type of the Excel file is "Application / VND.MS-Excel". To open an Excel document with servlet, you need to set the contentType in the Response object to "Application / VND.MS-Excel".
Response.setContentType (ContentType);
2. Content Disposition
The Content-Disposition in the HTTP Response Header allows the servlet to specify the information represented by the document. With this header, you can specify a document separately (instead of opening in the browser), and can also be displayed according to the user's operation.
If the user wants to save the document, you can also recommend a file name for the document. This suggestion name will appear in the File Name column of the Save AS dialog. If not specified, the name of the servlet will appear in the dialog.
In the servlet, set the header to the following:
Response.setHeader ("Content-Disposition", "Attachment; FileName =" "Example.xls");
Three points need to be described:
Ø Chinese file name needs to perform ISO8859-1 transcoders to display correctly:
FileName = new string (filename.getbytes ("gbk"), "ISO8859-1");
Ø The file name passed, needs to include the suffix name (if this file has a suffix name), otherwise the property of the file is lost, but cannot select the relevant program to open.
Ø Inquiry before downloading (Is it open or saved to a computer) and directly selects the relevant application plugin through the IE browser to open two ways, the former is as shown in the code, the latter is as follows:
Response.setHeader ("Content-Disposition", "FileName =" "Example.xls");
3. A number of experiences in the uploading and downloading of research documents
The I / O operation of the program is often the bottleneck of performance. Java IO defines two basic abstract classes: InputStream and OutputStream. For different data types such as disk, the network provides different implementations, Java.IO is also provided. Some bufferedstreams, enabling hard drives to read and write a large piece of data quickly, while Java basic I / O class can only read and write one byte, but bufferstream can read and write a batch at a time. Data, buffered stream greatly increases the performance of I / O. and so:
Ø Small block read / write data will be very slow, so try to read and write data
Ø Use BufferedInputStream and BufferedOutputStream to batch data to improve performance
Ø Serialization highly affects the performance of I / O, try less