java 작성 Http 서버 다운로드 도구

3169 단어 javaHttp다운로드
이 도구는 비교적 간단해서 다른 도구와 협조하여 파일을 전송하는 데 쓰인다. 쓸데없는 말은 하지 말고 코드를 올려라

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 도구
이상은 본고에서 기술한 모든 내용입니다. 여러분이 좋아하시기 바랍니다.
글을 친구에게 공유하거나 댓글을 남기는 데 시간이 좀 걸리세요.우리는 당신의 지지에 진심으로 감사할 것입니다.

좋은 웹페이지 즐겨찾기