java編寫Http服務(wù)器下載工具
這個工具比較簡單,用于配合另外一個工具進行文件傳送,廢話少說,上代碼
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(); } } }
這個工具實現(xiàn)了從HTTP服務(wù)器上下載指定行數(shù)的文件,并且不會因為編碼的問題引起下載的文件內(nèi)容亂碼
三個工具已經(jīng)搞定,下一次就是把這三個工具結(jié)合起來將HTTP、FTP的文件轉(zhuǎn)移到HDFS上
以上就是本文所述的全部內(nèi)容了,希望大家能喜歡。
請您花一點時間將文章分享給您的朋友或者留下評論。我們將會由衷感謝您的支持!
相關(guān)文章
分布式調(diào)度XXL-Job整合Springboot2.X實戰(zhàn)操作過程(推薦)
這篇文章主要介紹了分布式調(diào)度XXL-Job整合Springboot2.X實戰(zhàn)操作,包括定時任務(wù)的使用場景和常見的定時任務(wù),通過本文學習幫助大家該選擇哪個分布式任務(wù)調(diào)度平臺,對此文感興趣的朋友一起看看吧2022-04-04Java中Comparable和Comparator兩種比較器的區(qū)別詳解
這篇文章主要介紹了Java中Comparable和Comparator兩種比較器的區(qū)別詳解,Comparable接口將比較代碼嵌入自身類中,像Integer、String等這些基本類型的JAVA封裝類都已經(jīng)實現(xiàn)了Comparable接口,這些類對象本身就支持和自己比較,需要的朋友可以參考下2023-09-09SpringBoot+RabbitMQ實現(xiàn)消息可靠傳輸詳解
消息的可靠傳輸是面試必問的問題之一,保證消息的可靠傳輸主要在生產(chǎn)端開啟?comfirm?模式,RabbitMQ?開啟持久化,消費端關(guān)閉自動?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實現(xiàn)消息可靠傳輸,需要的可以參考一下2022-05-05Android bdflow數(shù)據(jù)庫神器的使用
這篇文章主要介紹了Android bdflow數(shù)據(jù)庫神器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03