| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1004 人关注过本帖
标题:网页下载文件的代码,尽量简单请问怎么写,我的代码不行,大家看看,怎么改 ...
只看楼主 加入收藏
sunpei
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-11-28
收藏
 问题点数:0 回复次数:1 
网页下载文件的代码,尽量简单请问怎么写,我的代码不行,大家看看,怎么改谢谢
程序代码:
<%@ page contentType="text/html;charset=UTF-8" %>     
<%@ page import = "*" %>
<%        

 //防止IE缓存
 response.setHeader("pragma","no-cache");

 response.setHeader("cache-control","no-cache");   

 response.setDateHeader("Expires",0);

 //设置编码

 //request.setCharacterEncoding("UTF-8");
 String fileName = new String(request.getParameter("fileName").getBytes("ISO-8859-1"),"UTF-8");

 //取到文件
 File file = new File(application.getRealPath("/")+"upload/" + fileName);
    

 response.reset();

 //response.setContentType("application/octet-stream;charset=UTF-8");
 response.setContentType("application/octet-stream;charset=UTF-8");//解决在弹出文件下载框不能打开文件的问题

 //System.out.println(response.getCharacterEncoding());

 //一定要对fileName进行encode

 //response.addHeader("Content-Disposition", "attachment; filename=" + (fileName, "UTF-8"));
 response.addHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("GBK"),"ISO8859-1"));//解决在弹出文件下载框不能打开文件的问题
 response.setContentLength((int) file.length());

 byte[] buffer = new byte[4096];

 BufferedOutputStream output = null;

 BufferedInputStream input = null;

 

 //写缓冲区
 try {
      output = new BufferedOutputStream(response.getOutputStream());
      out.clear();
         out = pageContext.pushBody();
      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) {

 }

 finally {
      if (input != null) input.close();
      if (output != null) output.close();

 }
%> 

图片附件: 游客没有浏览图片的权限,请 登录注册


[ 本帖最后由 sunpei 于 2011-12-9 13:03 编辑 ]
搜索更多相关主题的帖子: html 下载文件 网页 
2011-12-09 13:01
olkldksl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-4-19
收藏
得分:0 
我用html写了一个下载的页面,然后调用一个servlet执行下载操作,你看看能帮上你吗
html页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>下载页面</title>   
  </head>
  
  <body>
   
    请选择要下载的文件:<br/>
    <form action="/WEBTest/servlet/ServletDemo">
    <input type="radio" name="file" value="1.jpg" >文件1<br/>
    <input type="radio" name="file" value="2.jpg">文件2<br/>
    <input type="submit" value="提交">
    </form>
  </body>
</html>

servlet:
public class ServletDemo extends HttpServlet {

   
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //防止IE缓存
        response.setHeader("pragma","no-cache");
        response.setHeader("cache-control","no-cache");   
        response.setDateHeader("Expires",0);
        response.setHeader("content-disposition", "attachment;filename=");
        //设置编码
        request.setCharacterEncoding("UTF-8");
        String fileName = new String(request.getParameter("file").getBytes("ISO-8859-1"),"UTF-8");

        //取到文件完整路径
        String path = this.getServletContext().getRealPath(fileName);
        
        InputStream in = null;
        OutputStream out = null;
        //写缓冲区
        try {
            
            in = new FileInputStream(path);
            out = response.getOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len = in.read(buffer))>0){
                out.write(buffer,0,len);
            }
            
        }finally{
            if(in!=null)
                in.close();
            if(out!=null)
                out.close();
        }
            
    }

   
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}


2011-12-09 14:05
快速回复:网页下载文件的代码,尽量简单请问怎么写,我的代码不行,大家看看,怎 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044747 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved