国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

java編寫Http服務(wù)器下載工具

2019-11-26 15:15:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這個(gè)工具比較簡(jiǎn)單,用于配合另外一個(gè)工具進(jìn)行文件傳送,廢話少說(shuō),上代碼

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();    }  }}

這個(gè)工具實(shí)現(xiàn)了從HTTP服務(wù)器上下載指定行數(shù)的文件,并且不會(huì)因?yàn)榫幋a的問題引起下載的文件內(nèi)容亂碼
三個(gè)工具已經(jīng)搞定,下一次就是把這三個(gè)工具結(jié)合起來(lái)將HTTP、FTP的文件轉(zhuǎn)移到HDFS上

hadoop工具
ftp工具

以上就是本文所述的全部?jī)?nèi)容了,希望大家能喜歡。

請(qǐng)您花一點(diǎn)時(shí)間將文章分享給您的朋友或者留下評(píng)論。我們將會(huì)由衷感謝您的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵阳市| 安塞县| 肥西县| 大悟县| 罗田县| 龙里县| 商城县| 沙田区| 襄樊市| 曲阳县| 铜山县| 阜新| 女性| 永宁县| 吴忠市| 永年县| 佛学| 常宁市| 濮阳县| 长海县| 临夏县| 浮梁县| 新营市| 施秉县| 巴彦淖尔市| 克拉玛依市| 庄浪县| 霍林郭勒市| 四子王旗| 武穴市| 开封市| 麟游县| 同德县| 冕宁县| 分宜县| 嘉祥县| 九台市| 阳高县| 永丰县| 浮梁县| 依安县|