工具类—Java下载远程文件到本地
2018-02-02 14:36
316 查看
package com.alipay.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * @Name: RemoteFile.java * @Description: Java下载远程文件到本地。 * @Author: PeiFeng * @Create Date: 2017-8-12 */ public class RemoteFile { public static void main(String[] args) throws Exception { downRemoteFile( "https://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F060828381f30e9240ff2cd434c086e061d95f76a.jpg&thumburl=https%3A%2F%2Fss2.bdstatic.com%2F70cFvnSh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D2504031429%2C1727248259%26fm%3D26%26gp%3D0.jpg", "sssss.jpg", "D:/sss"); } /** * @Name: downRemoteFile。 * @Description: 下载远程文件。 * @Parameters: remoteFileUrl,要下载的远程文件地址。 * @Parameters: saveFileName,下载后保存的文件名。 * @Parameters: saveDir,下载后保存的文件路径。 * @Return: String,文件保存的地址。 * @Author: PeiFeng * @Version: V1.00 * @Create Date: 2017-8-12 */ public static String downRemoteFile(String remoteFileUrl, String saveFileName, String saveDir) { HttpURLConnection conn = null; OutputStream oputstream = null; InputStream iputstream = null; try { // 创建保存文件的目录 File savePath = new File(saveDir); if (!savePath.exists()) { savePath.mkdir(); } // 创建保存的文件 File file = new File(savePath + "/" + saveFileName); if (file != null && !file.exists()) { file.createNewFile(); } URL url = new URL(remoteFileUrl); // 将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接) // 此时cnnection只是为一个连接对象,待连接中 conn = (HttpURLConnection) url.openConnection(); // 设置是否要从 URL连接读取数据,默认为true conn.setDoInput(true); // 建立连接 // (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行) conn.connect(); // 连接发起请求,处理服务器响应 (从连接获取到输入流) iputstream = conn.getInputStream(); // 创建文件输出流,用于保存下载的远程文件 oputstream = new FileOutputStream(file); // 用来存储响应数据 byte[] buffer = new byte[4 * 1024]; int byteRead = -1; // 循环读取流 while ((byteRead = (iputstream.read(buffer))) != -1) { oputstream.write(buffer, 0, byteRead); } // 输出完成后刷新并关闭流 oputstream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { // 重要且易忽略步骤 (关闭流,切记!) if (iputstream != null) { iputstream.close(); } if (oputstream != null) { oputstream.close(); } // 销毁连接 if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } // 返回保存后的文件路径 return saveDir + "/" + saveFileName; } }转载 http://blog.csdn.net/u012758088/article/details/77119530?locationNum=4&fps=1
相关文章推荐
- java 远程从FTP服务器下载文件到本地
- java 远程从FTP服务器下载文件到本地
- java下载远程文件到本地
- java下载远程FTP文件至本地
- java下载远程文件到本地
- java 远程从FTP服务器下载文件到本地
- java 下载远程电脑文件到本地电脑
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- java下载远程文件到本地
- 批处理完成FTP下载远程备份文件到本地
- 从本地和远程复制文件java 代码
- java 下载远程文件
- [Java]远程下载文件并读取实例方法
- 从远程服务器下载文件到本地服务器
- java远程文件下载,支持文件断点续传
- JAVA远程下载文件 总结 思路~~Myself
- [Java]远程下载文件并读取实例方法
- php带密码功能并下载远程文件保存本地指定目录 修改加强版
- ASP.NET获取远程网页下载到本地文件