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();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有