首页 > 编程语言 > 详细

java 下载文件功能

时间:2014-04-08 20:14:22      阅读:509      评论:0      收藏:0      [点我收藏+]
public String downLoadFileContent() throws IOException{
         // 下载网络文件 
        int bytesum = 0;
        int byteread = 0;
        response = CustomerUtil.getResponse();
        request = CustomerUtil.getRequest();
        String filePath = request.getParameter("filePath");//http://localhost:8080/mypro/img
        String fileName = request.getParameter("fileName");//201212121212.txt
        URL url = new URL(filePath+fileName);
          
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            OutputStream fs=response.getOutputStream();
            //设置response的编码方式
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition","attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")); 
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


本文出自 “小浩” 博客,请务必保留此出处http://zhangchi.blog.51cto.com/5214280/1392282

java 下载文件功能,布布扣,bubuko.com

java 下载文件功能

原文:http://zhangchi.blog.51cto.com/5214280/1392282

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!