java下载远程文件到本地
2013-02-28 13:03
597 查看
/**
* 下载远程文件并保存到本地
* @param remoteFilePath 远程文件路径
* @param localFilePath 本地文件路径
*/
public void downloadFile(String remoteFilePath, String localFilePath)
{
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(localFilePath);
try
{
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
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();
}
}
}
* 下载远程文件并保存到本地
* @param remoteFilePath 远程文件路径
* @param localFilePath 本地文件路径
*/
public void downloadFile(String remoteFilePath, String localFilePath)
{
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(localFilePath);
try
{
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
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();
}
}
}
相关文章推荐
- java 下载远程电脑文件到本地电脑
- java 远程从FTP服务器下载文件到本地
- java下载远程文件到本地
- java 远程从FTP服务器下载文件到本地
- java 远程从FTP服务器下载文件到本地
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- java下载远程文件到本地
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- 工具类—Java下载远程文件到本地
- java下载远程FTP文件至本地
- 从本地和远程复制文件java 代码
- Java 从服务器下载文件到本地(页面、后台、配置都有)
- Java 批量文件压缩导出并下载到本地示例代码
- JAVA Ftp编程--远程下载文件
- python urllib从远程服务器下载文件到本地
- Java实现远程ssh执行shell脚本对文件打包下载
- linux远程文件下载java.net.UnknownHostException解决办法
- java本地文件下载功能
- java 通过SSH方式连接远程主机并上传和下载文件
- JAVA下载远程Linux服务器的文件