读书人

java http下载文件源

发布时间: 2012-07-24 17:47:58 作者: rapoo

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;

? ? }

? }

读书人网 >开源软件

热点推荐