Any type of file download will pop up the implementation of the file save box

xiaoxiao2021-03-06  39

:) :) :) Any type of file download will pop up the implementation of the file save box :( :( Introduction: Friends engaged in web programming must have encountered this situation, for some operating systems can be identified File Type If the link address of the file directly, the browser will automatically call the relevant program to open the file, such as .doc, .txt, .html, etc., there is still something to expose the file directly to the user, no It is better to confidential to documents. Therefore, it is necessary to provide a method to improve this situation. I have found a way through practice, I hope everyone will discuss it.

Experimental Environment: Ø Platform: Windows 2000 Advanced Serverø Web Server: Tomcat4.0.4ø JDK: JDK1.3.1 Implementation Method: I am engaged in Java development, so trying to use servlet to implement this feature, now I am attached to the following, for Everyone refers to package com.timeschina.servlet; import javax.servlet. *; import javax.servlet.http. *; Import java.io. *; Import java.util. *; Import com.timeschina.common. *; / * **

Title: File download servlet *

Description: file download servlet *

Copyright: Copyright (C) 2002 *

Company: Tiger, . Ltd * @author TIM * @version 1.0 * / public class DownloadServlet extends HttpServlet {static final private String CONTENT_TYPE = "text / html; charset = GBK"; // Initialize global variablespublic void init () throws ServletException { } // Process the HTTP Get requestpublic void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html; charset = gb2312"); HttpSession session = request.getSession (); // to download The reason why the file name // is mainly due to the Chinese version of the file name, the Chinese file name will also display string downloadfile = new string, "(Request.GetParameter (" downfile "). GetBytes (" Downfile "). GetBytes "ISO8859-1"), "GB2312"); servletContext context (); servletconfig = getServletConfig (); // Get the directory where you want to download file, here is a format corresponding to the physical path // directory on the server Is this: // Root Catalog (the actual physical directory corresponding to the web main catalog) // virtual directory (subdirectory stored by download file) DownloadPath = context.getRealPath (file.separator) Config.GetinitParameter ("DownloadPath" ) File.separator; // Build the object java.io.file file = new java.io.file (DownloadPath DownloadFile); // LONG FileSize = file.length ();

// Set the output format Response.addhead ("Content-Type", "Application / X-msdownload;" "); Response.addheader (" Content-Disposition "," Attachment; FileName = " Response.EncodeURL (DownloadFile); Response.addheader ("Content-length", long.toTOString (filesize)); / / write file java.io.fileinputStream Fin = new java.io.fileinputStream (file); byte [] b = new byte [1]; int J = 0; while ((j = fin.read (b))> 0) {response.getOutputStream (). Write (b);} Fin.close ();} // process the http post requestpublic void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} // Clean up resourcespublic void destroy () {}} the following is the content of WEB.XML SERVLET profile: DownloadServlet com.timeschina.servlet.downloadServlet DownloadPath

Download DownloadServlet / downloadservlet 20 The following is the example of calling the servlet Test

转载请注明原文地址:https://www.9cbs.com/read-76976.html

New Post(0)