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] == '
'){
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로 옮기는 것입니다.
hadoop 도구 ftp 도구
이상은 본고에서 기술한 모든 내용입니다. 여러분이 좋아하시기 바랍니다.
글을 친구에게 공유하거나 댓글을 남기는 데 시간이 좀 걸리세요.우리는 당신의 지지에 진심으로 감사할 것입니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.