java 下载文件功能

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 下载文件功能

上一篇:转载:c++读写文件和测试程序运行时间的例子


下一篇:JavaScript 表单验证