<%@page import="java.io.*"%><%
downloadFile("c:\\mplayer.exe","application/x-zip-compressed","downloaded.zip",1024,response);
%><%!
public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize,HttpServletResponse response)
        throws IOException, ServletException
    {
        String m_contentDisposition = new String(); File file = new File(sourceFilePathName);
        FileInputStream fileIn = new FileInputStream(file);
        long fileLen = file.length();
        int readBytes = 0;
        int totalRead = 0;
        byte b[] = new byte[blockSize];
        
//&#65422;&#65412;&#65404;&#63730;&#65408;獎&#65421;
if(contentType == null)
            response.setContentType("application/x-msdownload");
        else
        if(contentType.length() == 0)
            response.setContentType("application/x-msdownload");
        else
            response.setContentType(contentType);        //&#65395;&#65380;&#65398;&#65416;
response.setContentLength((int)fileLen);
        
//
m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
        if(destFileName == null)
            response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append( getFileName(sourceFilePathName) )));
        else
        
//&#65423;&#65410;&#65428;&#65432;&#65402;&#65412;&#65422;&#65412;&#65404;&#63730;&#65411;&#12539;
if(destFileName.length() == 0)
            response.setHeader("Content-Disposition", m_contentDisposition);
        else
            response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(destFileName)));
        
while((long)totalRead < fileLen) 
        {
            readBytes = fileIn.read(b, 0, blockSize);
            totalRead += readBytes;
            response.getOutputStream().write(b, 0, readBytes);
        }
        fileIn.close();
    }private String getFileName(String filePathName)
    {
        String token = new String();
        String value = new String();
        int pos = 0;
        int i = 0;
        int start = 0;
        int end = 0;
        pos = filePathName.lastIndexOf('/');
        if(pos != -1)
            return filePathName.substring(pos + 1, filePathName.length());
        pos = filePathName.lastIndexOf('\\');
        if(pos != -1)
            return filePathName.substring(pos + 1, filePathName.length());
        else
            return filePathName;
    }
%>

解决方案 »

  1.   

    response.setContentType("application/x-msdownload");
      

  2.   

    你只要把contentType设置成application/octet-stream就好了
      

  3.   

    WORD文档还是直接打开的,而不出现下载对话框。 而且如果是txt都不能打开报以下错误org.apache.jasper.JasperException: getOutputStream() has already been called for this response
      

  4.   

    提供一个java端的函数,楼主可慢慢参考。
        
      public void downloadFile(HttpServletResponse res,
                               String pathName, //路径文件名
                               String fileName, //下载初始默认文件名
                               String contentType) throws IOException {
        
        InputStream inputStream  = new FileInputStream(pathName);
        res.setHeader("Pragma", "");
        res.setHeader("Cache-Control", "");
        res.setContentType(contentType);
        
        String fnEncode = new String(fileName.getBytes("gb2312"), "8859_1");
        res.setHeader("Content-Disposition", "attachment; filename="+fnEncode);
        res.setHeader("Cache-Control", "max-age=0");    int size = 0;
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
          input = new BufferedInputStream(inputStream);
          output = new BufferedOutputStream(res.getOutputStream());
          int c;
          while ((c = input.read()) != -1) {
            output.write(c);
            size++;
          }
          output.flush();
          res.getOutputStream().flush();
        } finally {
          if (input != null) {
            input.close();
          }
          if (output != null) {
            output.close();
          }
        }  }
      

  5.   

    我找到一种方法,用的是jspsmartupload组件里的下载方法。然后在前面使用setContentDisposition(null);方法设定contentDisposition为null以禁止浏览器自动打开文件,保证点击链接后是下载文件。但是还有一个问题存在。链接的文件是txt类型还是会报刊org.apache.jasper.JasperException: getOutputStream() has already been called for this response的错误,实在不知道这个问题应该怎么解决