0009-下载远程文件
2016-02-03 10:08
288 查看
/**
*
* Description:通过配置IP和端口,下载对应的资源文件
*
* @author: chenfengyi
* @param file
* @return
*
*/
public boolean downLoadFromRemote(String file, String ip, String port) {
downloadPath = Thread.currentThread()
.getContextClassLoader().getResource("").getPath() + file;
String strURL = "http://" + ip + ":" + port + "/csv/downcsv.php?fileName=" + file;
LOGGER.info("开始下载资源文件[" + strURL + "],请等待……");
boolean ret = this.getRemoteFile(strURL, downloadPath);
if (ret) {
LOGGER.info("下载资源文件[" + file + "]完成");
return true;
}
LOGGER.info("下载资源[" + file + "]文件失败");
return false;
}
/**
* 通过HTTP方式获取文件,默认编码格式
*
* @param strUrl
* @param fileName
* @return
获取成功 true 失败 false
* @throws IOException
*/
private boolean getRemoteFile(String strUrl, String fileName){
DataInputStream input = null;
DataOutputStream output = null;
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
input = new DataInputStream(conn.getInputStream());
output = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[1024 * 8];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
return true;
} catch (IOException e) {
LOGGER.warn(e.getMessage());
return false;
} finally{
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
*
* Description:通过配置IP和端口,下载对应的资源文件
*
* @author: chenfengyi
* @param file
* @return
*
*/
public boolean downLoadFromRemote(String file, String ip, String port) {
downloadPath = Thread.currentThread()
.getContextClassLoader().getResource("").getPath() + file;
String strURL = "http://" + ip + ":" + port + "/csv/downcsv.php?fileName=" + file;
LOGGER.info("开始下载资源文件[" + strURL + "],请等待……");
boolean ret = this.getRemoteFile(strURL, downloadPath);
if (ret) {
LOGGER.info("下载资源文件[" + file + "]完成");
return true;
}
LOGGER.info("下载资源[" + file + "]文件失败");
return false;
}
/**
* 通过HTTP方式获取文件,默认编码格式
*
* @param strUrl
* @param fileName
* @return
获取成功 true 失败 false
* @throws IOException
*/
private boolean getRemoteFile(String strUrl, String fileName){
DataInputStream input = null;
DataOutputStream output = null;
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
input = new DataInputStream(conn.getInputStream());
output = new DataOutputStream(new FileOutputStream(
fileName));
byte[] buffer = new byte[1024 * 8];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
return true;
} catch (IOException e) {
LOGGER.warn(e.getMessage());
return false;
} finally{
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- secureCRT 实现 xp ssh登录 centos(vbox虚拟机)
- Input and Output
- 玩转Oracle之12c 可插拔数据库数据泵功能体验
- linux指定某非root用户执行开机启动项的方法
- Oralce函数经典 - 日期函数-日期加减法 -(咋个办呢 zgbn)
- Notepad++ 16进制编辑功能
- 实用软件
- 随想28:愿我成为一个高级黑
- Vuforia切换回识别场景后黑屏解决
- php面向对象之克隆对象
- 在accept前调用fork和accept后调用fork的区别
- linux环境下实现图片的读取和显示
- Java Mail 530 5.7.1 Client was not authenticated 错误解决
- xss过滤函数
- vim快捷键整理
- 初识结构类型小练习
- git地址
- Android NDK开发(1)----- Java与C互相调用实例详解
- hdu 1850 Being a Good Boy in Spring Festival
- eclipse安装svn插件