標籤:read long 實現 span void jpg 輸出資料流 cat close
用java打造屬於自己的爬蟲
網路上的圖片太多 一個一個的儲存實在太浪費時間
基於此 就使用了java做了一個小工具
功能是檔案的複製 以及 網路片的下載
首先是IOUtlis類的建立
package hh;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.Reader;import java.io.Writer;import java.net.*;public class IOUtils {//buff緩衝區public static void closeQuietly(FileOutputStream outpustream){ if(outpustream!=null) { try{ outpustream.close();} catch (IOException e){// TODO 自動產生的 catch 塊//e.printStackTrace();} } }public static void closeQuietly(FileInputStream inStream){ if(inStream!=null) { try{inStream.close();} catch (IOException e){// TODO 自動產生的 catch 塊//e.printStackTrace();} } }public static void closeQuietly(Reader reader){ if(reader!=null) { try { reader.close(); } catch(IOException e ) { // } } }public static void copy(InputStream instream,OutputStream outputstream,int buff) throws IOException{byte[] buffer=new byte[buff];int len;//計算讀入的資料if(instream==null){throw new IllegalArgumentException("INPUTSTREAM不可為空");}if(outputstream==null){throw new IllegalArgumentException("outputstream不可為空");}if(buff<=0){throw new IllegalArgumentException("outputstream不可為空");}while((len=instream.read(buffer))>0){//不知道怎麼處理異常就讓調用者處理outputstream.write(buffer,0,len);}}public static void copy(InputStream instream,OutputStream outputstream) throws IOException{copy(instream,outputstream,1024);}
copy這個方法實現了檔案的複製功能,closequietly將檔案流安靜的關閉
方案一
檔案的複製,目的是將E盤的1.txt檔案複製到W盤的2.txt檔案裡 最後不要忘了關閉檔案的輸入輸出資料流
不然可能導致複製失敗
package hh;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class test {public static void main(String[] args){long startMS=System.currentTimeMillis();FileInputStream fis=null;FileOutputStream fos=null;try{fis=new FileInputStream("E:\\1.txt");fos=new FileOutputStream("W:\\2.txt");IOUtils.copy(fis,fos,1024);long endMS=System.currentTimeMillis();System.out.println("拷貝完成 耗時"+(endMS-startMS));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
方案二
調用了java.net.*;這個包 使用url類將圖片地址比如畫圈部分當做一個檔案的輸入資料流來使用,再使用copy方法將圖片複製到本地。最後我發現該網站所有的圖片都有一個特定的圖片遞增規則(可以考慮使用Regex)這樣就可以通過變數來完成網站裡所有的圖片的複製
package hh;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.*;public class abc {public static void main(String []args){ InputStream inpustream=null; FileOutputStream outputstream=null;try {long start=System.currentTimeMillis();int i=0;for(i=1;i<550;i++){URL url=new URL("http://www.bokee.com//topic/images/%E6%9C%AA%E6%A0%87%E9%A2%98-2("+i+").jpg"); inpustream=url.openStream(); // http://www.bokee.com//topic/images/%E6%9C%AA%E6%A0%87%E9%A2%98-1(530).jpg// <img src="/_upload/article/images/66/b6/810dfe294d62aa81fba5ca1baef9/57c958b6-72e8-4331-a431-24b06039b649.jpg">// <img src="/_upload/article/images/99/a1/416529614b32ba6a2f824bbbafae/04b85865-f1b9-4128-b33f-04eff1e81c5a.jpg"> outputstream=new FileOutputStream("W://main//"+i+".png"); IOUtils.copy(inpustream, outputstream);}long end=System.currentTimeMillis();System.out.println("下載成功,耗時"+(start-end)+"毫秒");} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
成果如下,最終滿足了自己的需求,節省了時間。
JAVA之檔案複製 圖片下載