<a href="urfile">click here to download</a>

解决方案 »

  1.   

    public void downloadFile(String sourceFilePathName, String contentType, String destFileName,
                                 int blockSize) throws IOException, ServletException{
            if(sourceFilePathName == null)
                throw new IllegalArgumentException("File '"+sourceFilePathName+"' not found.");
            if(sourceFilePathName.equals(""))
                throw new IllegalArgumentException("File '"+sourceFilePathName+"' not found.");
            if(isVirtual(sourceFilePathName))
                sourceFilePathName = m_application.getRealPath(sourceFilePathName);
            File file = new File(sourceFilePathName);
            if(!file.exists())
                throw new IllegalArgumentException("File '"+sourceFilePathName+"' not found.");        FileInputStream fileIn = new FileInputStream(file);
            long fileLen = file.length();
            int readBytes = 0;
            int totalRead = 0;
            byte b[] = new byte[blockSize];
            if(contentType == null)
                m_response.setContentType(downContentType);
            else
            if(contentType.length() == 0)
                m_response.setContentType(downContentType);
            else
                m_response.setContentType(contentType);
            m_response.setContentLength((int)fileLen);        m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
            if(destFileName == null)
                m_response.setHeader("Content-Disposition", m_contentDisposition+" filename="+getFileName(sourceFilePathName));
            else
            if(destFileName.length() == 0)
                m_response.setHeader("Content-Disposition", m_contentDisposition);
            else
                m_response.setHeader("Content-Disposition", m_contentDisposition+" filename="+destFileName);
            while((long)totalRead < fileLen){
                readBytes = fileIn.read(b, 0, blockSize);
                totalRead += readBytes;
                m_response.getOutputStream().write(b, 0, readBytes);
            }
            fileIn.close();
        }
      

  2.   

    zip,rar文件可以用第一种方法简单。
    txt,doc,...就得用后一种方法啦。