首页 > 代码库 > java http下载文件

java http下载文件

private boolean downloadFile(String httpUrl, String savePath) {        int byteread = 0;        try {            URL url = new URL(httpUrl);            URLConnection conn = url.openConnection();            InputStream inStream = conn.getInputStream();            FileOutputStream fs = new FileOutputStream(savePath);            byte[] buffer = new byte[1204];            while ((byteread = inStream.read(buffer)) != -1) {                fs.write(buffer, 0, byteread);            }            System.out.println(savePath+" download finished!");            return true;        } catch (MalformedURLException e) {            e.printStackTrace();            return false;        } catch (IOException e) {            e.printStackTrace();            return false;        }    }

 

java http下载文件