1. 程式人生 > >JAVA之文件復制 圖片下載

JAVA之文件復制 圖片下載

處理 tar malformed void close pub 自動 width 自己

用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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
	}

  方案二

調用了java.net.*;這個包 使用url類將圖片地址比如下圖畫圈部分當做一個文件的輸入流來使用,再使用copy方法將圖片復制到本地。最後我發現該網站所有的圖片都有一個特定的圖片遞增規則(可以考慮使用正則表達式)這樣就可以通過變量來完成網站裏所有的圖片的復制

技術分享

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 block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
	
	
	
	}
	

  

成果如下,最終滿足了自己的需求,節省了時間。

技術分享

JAVA之文件復制 圖片下載