java远程下载文件到本地
方法一
**
* 下载远程文件并保存到本地
*
* @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 website = null;
ReadableByteChannel rbc = null;
FileOutputStream fos = null;
try {
website = new URL(remoteFilePath);
rbc = Channels.newChannel(website.openStream());
fos = new FileOutputStream(localFilePath);//本地要存储的文件地址 例如:test.txt
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(rbc!=null){
try {
rbc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
java远程下载文件到本地的更多相关文章
- Java远程下载文件到本地(http协议和ssh2协议)
Java中java.io包为我们提供了输入流和输出流,对文件的读写基本上都依赖于这些封装好的关于流的类中来实现.前段时间遇到了以下两种需求: 1.与某系统对接,每天获取最新的图片并显示在前端页面.该系 ...
- PHP实现远程下载文件到本地
PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载 经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经 ...
- PHP CURL实现远程下载文件到本地
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...
- java实现下载文件到本地
代码如下: URL url = new URL("http://www.cnblogs.com/images/logo_small.gif"); URLConnection con ...
- 使用xshell从远程服务器下载文件到本地
XSHELL工具上传文件到Linux以及下载文件到本地(Windows) Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的L ...
- 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)
在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...
- C#使用WebClient下载文件到本地目录
C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...
- Js点击按钮下载文件到本地(兼容多浏览器)
实现点击 用纯 js(非jquery) 下载文件到本地 自己尝试,加网上找了好久未果,如: window.open(url) location.href=url form表单提交 ifr ...
- 【转】XSHELL下直接下载文件到本地(Windows)
XSHELL下直接下载文件到本地(Windows) http://www.cnblogs.com/davytitan/p/3966606.html
随机推荐
- Jquery 简明介绍
http://www.cnblogs.com/luotianshuai/p/5196997.html http://www.cnblogs.com/liujianzuo888/articles/568 ...
- Hadoop 编写WordCount
本文发表于本人博客. 前面几次讲了关于Hadoop的环境搭建.HDFS操作,今天接着继续.本来Hadoop源码中就有一个例子WordCount,但是今天我们来自己实现一个加深对这个Mapper.Red ...
- linux 常用命令总结(二)
1. linux下以指定的编码打开文件:LANG=zh_CN vi fileName 2. 查看系统内存使用,可以使用free -m 或 top 3. 使用env查看所有环境变量 4. df –h 查 ...
- PHP保存Base64图片base64_decode的问题 文件打不开的问题
PHP对Base64的支持非常好,有内置的base64_encode与base64_decode负责图片的Base64编码与解码. 编码上,只要将图片流读取到,而后使用base64_encode进 ...
- Linux内核参数之arp_ignore和arp_announce
一.arp_ignore和arp_announce介绍 arp_ignore和arp_announce参数都和ARP协议相关,主要用于控制系统返回arp响应和发送arp请求时的动作.这两个参数很重要, ...
- bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 按海拔是否相同分块 每次bfs海拔相 ...
- 微信小程序:WXML 模板
微信小程序:WXML 模板 一.WXML 模板 网页编程采用的是 HTML + CSS + JS 这样的组合,其中 HTML 是用来描述当前这个页面的结构,CSS 用来描述页面的样子,JS 通常是用来 ...
- 在pom.xml中使用distributionManagement将项目打包上传到nexus私服
本文介绍 如何在pom.xml中使用distributionManagement将项目打包上传到nexus私服 1.pom.xml文件添加distributionManagement节点 <!- ...
- APP AutoTestCaseID
public class AutoTestCaseID { ElementExist el = new ElementExist(); static AutoTestExcelFile ft = ne ...
- jmeter 网速
有人知道在jmeter 哪个里面哦 JMeterPlugins里面 network