欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java用文件流下载网络文件示例代码

程序员文章站 2024-02-16 13:20:46
复制代码 代码如下:public httpservletresponse download(string path, httpservletresponse respons...

复制代码 代码如下:

public httpservletresponse download(string path, httpservletresponse response) {
        try {
            // path是指欲下载的文件的路径。
            file file = new file(path);
            // 取得文件名。
            string filename = file.getname();
            // 取得文件的后缀名。
            string ext = filename.substring(filename.lastindexof(".") + 1).touppercase();

            // 以流的形式下载文件。
            inputstream fis = new bufferedinputstream(new fileinputstream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的header
            response.addheader("content-disposition", "attachment;filename=" + new string(filename.getbytes()));
            response.addheader("content-length", "" + file.length());
            outputstream toclient = new bufferedoutputstream(response.getoutputstream());
            response.setcontenttype("application/octet-stream");
            toclient.write(buffer);
            toclient.flush();
            toclient.close();
        } catch (ioexception ex) {
            ex.printstacktrace();
        }
        return response;
    }

    public void downloadlocal(httpservletresponse response) throws filenotfoundexception {
        // 下载本地文件
        string filename = "operator.doc".tostring(); // 文件的默认保存名
        // 读到流中
        inputstream instream = new fileinputstream("c:/operator.doc");// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setcontenttype("bin");
        response.addheader("content-disposition", "attachment; filename=\"" + filename + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = instream.read(b)) > 0)
                response.getoutputstream().write(b, 0, len);
            instream.close();
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }

    public void downloadnet(httpservletresponse response) throws malformedurlexception {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;

        url url = new url("windine.blogdriver.com/logo.gif");

        try {
            urlconnection conn = url.openconnection();
            inputstream instream = conn.getinputstream();
            fileoutputstream fs = new fileoutputstream("c:/abc.gif");

            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = instream.read(buffer)) != -1) {
                bytesum += byteread;
                system.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }