java编写Http服务器下载工具
更新时间:2015年03月25日 15:39:28 投稿:hebedich
这篇文章主要介绍了java编写Http服务器下载工具的方法,工具很简单,功能也很简单,代码就更简洁了,却非常实用,有需要的小伙伴参考下吧。
这个工具比较简单,用于配合另外一个工具进行文件传送,废话少说,上代码
import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class HttpUtil{
private String httppath = "";
public void setHttpPath(String httppath){
this.httppath = httppath;
}
public String getHttpPath(){
return this.httppath;
}
public HttpUtil(String httppath){
this.httppath = httppath;
}
public InputStream getStream(String url){
InputStream inStream = null;
try{
URL httpurl = new URL(url);
URLConnection conn = httpurl.openConnection();
inStream = conn.getInputStream();
}catch (Exception e){
e.printStackTrace();
return null;
}
return inStream;
}
public int downLoad(String url,String localName ,int lines) throws FileNotFoundException, IOException{
FileOutputStream fos = null;
InputStream inStream = null;
int ret = 0;
try{
URL httpurl = new URL(url);
URLConnection conn = httpurl.openConnection();
inStream = conn.getInputStream();
fos = new FileOutputStream(localName);
byte[] b = new byte[102400];
int j = 0;
while(inStream.read(b) != -1 && lines > 0){
for(int i = j; i < b.length; i++){
if(b[i] == '\n'){
fos.write(b, j, i - j + 1);
lines--;
if(lines <= 0){
break;
}
j = i + 1;
continue;
}
}
}
}catch (Exception e){
e.printStackTrace();
ret = -1;
}finally {
fos.close();
inStream.close();
return ret;
}
}
public static void main(String[] args){
String httppath = "";
int lines = 0;
String localName = "";
try{
httppath = args[0];
localName = args[1];
lines = Integer.parseInt(args[2]);
}catch (Exception e){
e.printStackTrace();
return;
}
try{
HttpUtil hu = new HttpUtil(httppath);
hu.downLoad(hu.getHttpPath(),localName ,lines);
}catch (Exception e){
e.printStackTrace();
}
}
}
这个工具实现了从HTTP服务器上下载指定行数的文件,并且不会因为编码的问题引起下载的文件内容乱码
三个工具已经搞定,下一次就是把这三个工具结合起来将HTTP、FTP的文件转移到HDFS上
以上就是本文所述的全部内容了,希望大家能喜欢。
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!
相关文章
Java并发容器之ConcurrentLinkedQueue详解
这篇文章主要介绍了Java并发容器之ConcurrentLinkedQueue详解,加锁队列的实现较为简单,这里就略过,我们来重点来解读一下非阻塞队列,2023-12-12
从点到面, 下面我们来看下非阻塞队列经典实现类ConcurrentLinkedQueue,需要的朋友可以参考下
SpringCloud Config使用本地仓库及map注入
这篇文章主要介绍了SpringCloud Config使用本地仓库及map注入,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-09-09
SpringCloud使用Nacos保存和读取变量的配置方法
在使用SpringCloud开发微服务时,经常会遇到一些比较小的后台参数配置,这些配置不足以单独开一张表去存储,而且其他服务会读取该参数,这篇文章主要介绍了SpringCloud使用Nacos保存和读取变量,需要的朋友可以参考下2022-07-07


最新评论