首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

java http下载文件源

2012-07-24 
java http下载文件流?/**? ? * 通过HTTP 协议获取文件流? ? * @param urlPath http协议的url? ? * @param

java http下载文件流

?/**

? ? * 通过HTTP 协议获取文件流

? ? * @param urlPath http协议的url

? ? * @param fileNamePath 文件路径

? ? * @return true:写入成功?

? ? */

? ?public ?boolean saveFileToHttp(String urlPath, String fileNamePath) {

? ? try {

? ? ? URL url = new URL(urlPath);

? ? ? HttpURLConnection connection = (HttpURLConnection) url.openConnection();

? ? ? DataInputStream in = new DataInputStream(connection.getInputStream());

? ? ? DataOutputStream out = new DataOutputStream(new FileOutputStream(fileNamePath));

? ? ? byte[] buffer = new byte[4096];

? ? ? int count = 0;

? ? ? while ((count = in.read(buffer)) > 0) {

? ? ? ? out.write(buffer, 0, count);

? ? ? }

? ? ? out.close();

? ? ? in.close();

? ? ? return true;

? ? }

? ? catch (Exception e) {

? ? ? return false;

? ? }

? }

热点排行