1. Download the file in two ways: one is a hyperlink, one is Servlet available for download.
2, a hyperlink to download: When files can be opened directly on the page, will open the file directly, instead of downloading, when the file can not be opened, will be available for download window.
3, hyperlinks to download the schematic
4, the principle Servlet download files
5, sample code
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { the this .doPost (Request, Response); } protected void the doPost (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException { String filename = request.getParameter ( "filename"); // . 1 acquires the file name to be downloaded // 2 file folder where the relative path converted into the absolute path String = the folderPath the this .getServletContext () the getRealPath ( "downloads." ); // . 3 stream is used to obtain the input file on the server to read out the InputStream iS = new new the FileInputStream (the folderPath + "/ "+ filename); // 4 Add the appropriate information to the head role is to pop up a dialog box to save the file //response.setHeader ( "Content-Disposition", "Attachment; filename =" + filename); // Chinese file name download dialog box will be garbled. Treatment: Different treatments IE and other browsers // IE: // response.setHeader ( "the Content-Disposition", "Attachment; filename =" + the URLEncoder.encode (filename, "UTF-. 8")) ; // FireFox String = newfilename new new String (filename.getBytes ( "UTF-. 8"), "ISO-8859-1" ); response.setHeader ( "the Content-Disposition", "Attachment; filename =" + newfilename); @ 5 is written to the local disk on the OutputStream OS = response.getOutputStream (); int len = 0 ; new byte[1024]; while((len=is.read(b))!=-1){ os.write(b,0, len); } is.close(); os.close(); } }