各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。!

解决方案 »

  1.   

    <a href="abc.rar">download abc</a>
      

  2.   

    <tr>
                <td height="25" class="tdcor">附&nbsp;&nbsp;件&nbsp;</td>
                <td colspan="3" height=50>
                    <%
                        if (null != publish.getAttatchFilename() &&
    publish.getAttatchFilename().length() > 0) {
                    %>
                    <a href="publish_do.jsp?method=download&fileName=
    <%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>">
    <%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%></a>
                    <%
                        }
                    %>
                </td>
    </tr>
         <% else if (null != method && method.equals("download")) {//下载附件        String fileName = request.getParameter("fileName");
            File file = new File(Constants.PUBLISH_FILE_PATH + "/" + URLDecoder.decode(fileName,"GBK"));
            response.reset();
            response.setContentType("application/octet-stream; charset=GBK");
            response.addHeader("Content-Disposition", "attachment; filename=" + CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1"));
            response.setContentLength((int) file.length());        byte[] buffer = new byte[4096];
            BufferedOutputStream output = null;
            BufferedInputStream input = null;        // 写缓冲区:
            try {
                output = new BufferedOutputStream(response.getOutputStream());
                input = new BufferedInputStream(new FileInputStream(file));            int n = (-1);
                while ((n = input.read(buffer, 0, 4096)) > -1) {
                    output.write(buffer, 0, n);
                }
                response.flushBuffer();
            }
            catch (Exception e) {
            } // maybe user cancelled download
            finally {
                if (input != null) input.close();
                if (output != null) output.close();
            }
      

  3.   

    这样可以下载,但是我希望是用java代码写的,可以下载任何文件格式的文件!
    不过仍然很感谢你!
      

  4.   

    <%@ page language="java"  pageEncoding="GB18030"%>
    <%
    out.clear();
    out = pageContext.pushBody(); 
      String fileName = request.getParameter("filename");  InputStream inStream=new FileInputStream(fileName);
     //设置输出的格式
      response.reset();
      response.setContentType("bin");
      response.addHeader("Content-Disposition","attachment; filename=\"" + ruleName + "\"");
     //循环取出流中的数据
      byte[] b = new byte[100];
      int len;
      while((len=inStream.read(b)) >0)
      response.getOutputStream().write(b,0,len);  
      inStream.close();
      out.clear(); 
      out = pageContext.pushBody(); 
    %>