From Mengxian's big blog
Web developers have such questions, how to let a file, especially a known type of file, send to the client, directly prompting the viewer to download, instead of using the program associated with it. Previously we used the most common way to add such a file to the link, so that the viewer can save the files to download the files by clicking the target of the mouse button. However, there are two indi-sufficient places: First, if the browser can identify the extension of the downloaded file, the browser activates the program associated with the extension to open the downloaded file. For example: on the Windows platform, if the user clicked the link link is a ".doc" file, then the browser will start Microsoft Word application to open it. The second is: If you use a link, anyone who can see the link can download the file. Although you can also set the files to the file, it is not very convenient. Sometimes we need more flexible and elastic way, the following procedures can easily overcome the shortcomings of the above two aspects. This approach is reliable, but you must remember that the unauthorized user cannot obtain the copy of the file by entering the URL of the file in the browser address bar. So, the file to download should be placed in a directory outside of the virtual directory, such as: If your virtual directory is c: / mengxianhui / tomcat4 / website / myApp, then store any of the directory and the directory All files on the subdirectory are visible to any user on the Internet. To download a file directly, we need to do two things, the first thing is: Set the content class "Application / OcTet-stream", case-sensitive. The second thing is: Setting the HTTP response head name: Content-Disposition, the set value is: attachment; filename = thefilename. The thefilename here is the default file name in the file download dialog, usually the same as the downloaded file name, but can also be different. Below, we will give an example of practical application on the JSP and ASP pages that usually use. An example of the testfiledownload.jsp page: <% // Get file name and path string filename = "mengxianhuaiDOCTEST.DOC"; string filepath = "d: //"; // Set the response head and download saved file name response.setContentType "Application / Oct-stream"); Response.setHeader ("Content-Disposition", "Attachment; FileName = /" " filename " / "); // Open the flow information of the specified file java.io.fileInputStream FileInputStream = New java.io.fileinputStream (filepath filename); // Write flow information INT i; while ((i = fileinputstream.read ())! = -1) {Out.write (i);} fileInputstream.close (); out.close ();%> It is worth noting that in addition to the file content you want to download, other characters should be additionally attached, including spaces and carriage return.
When we are writing code, we often add some spaces, tabs, or regular retractions, which seems to be clear, but sometimes does not have the correct result. For example: <% @ page import = "java.io. *"%>