java 远程文件 下载

来源:互联网 发布:php html页面生成图片 编辑:程序博客网 时间:2024/05/16 19:18
/**远程文件下载 * @param remoteFilePath 下载地址 * @param response * @author DanaHe */public static void downloadFile(String remoteFilePath,HttpServletResponse response) {        URL urlfile = null;        HttpURLConnection httpUrl = null;        BufferedInputStream bis = null;        ServletOutputStream bos = null;        try        {            urlfile = new URL(remoteFilePath);            httpUrl = (HttpURLConnection)urlfile.openConnection();            httpUrl.connect();            bis = new BufferedInputStream(httpUrl.getInputStream());            bos = response.getOutputStream();            int len = 2048;            byte[] b = new byte[len];            while ((len = bis.read(b)) != -1)            {                bos.write(b, 0, len);            }            bos.flush();            bis.close();            httpUrl.disconnect();        }        catch (Exception e)        {            e.printStackTrace();        }        finally        {            try            {                bis.close();                bos.close();            }            catch (IOException e)            {                e.printStackTrace();            }        }    }