(一)這里可以先復習一下java輸入輸出流和文件操作---
1.File類保存文件或目錄的各種元數據信息,包括文件名、文件長度、最后修改時間、是否可讀、獲取當前文件的路徑名。判斷指定文件是否存在、獲取當前目錄中的文件列表,創建、刪除文件和目錄等。
2.I/O流 根據處理數據類型的不同分為:字符流和字節流
根據數據流向不同分為:輸入流和輸出流
(二)在JAVA里面,可以用復制語句”A=B”給基本類型的數據傳遞值,但是如果A,B是兩個同類型的數組,復制就相當於將一個數組變量的引用傳遞給另一個數組;如果一個數組發生改變,那么引用同一數組的變量也要發生改變.
以下是歸納的JAVA中復制數組的方法:
1.使用FOR循環,將數組的每個元素復制或者復制指定元素,不過效率差一點
2.使用clone方法,得到數組的值,而不是引用,不能復制指定元素,靈活性差一點
3.使用System.arraycopy(src, srcPos, dest, destPos, length)方法,推薦使用
舉例:
1.使用FOR循環
int[] src={1,3,5,6,7,8};
int[] dest = new int[6];
for(int i=0;i<6;i++) dest[i] = src[i];
2.使用clone
int[] src={1,3,5,6,7,8};
int[] dest;
dest=(int[]) src.clone();//使用clone創建
副本,注意clone要使用強制轉換
3.使用System.arraycopy
int[] src={1,3,5,6,7,8};
int[] dest = new int[6];
System.arraycopy(src, 0, dest, 0, 6);
——————————————————————-
System提供了一個靜態方法arraycopy(),我們可以使用它來實現數組之間的復制.
其函數原型是:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src:源數組; srcPos:源數組要復制的起始位置;
dest:目的數組; destPos:目的數組放置的起始位置;
length:復制的長度.
注意:src and dest都必須是同類型或者可以進行轉換類型的數組.
有趣的是這個函數可以實現自己到自己復制,
比如:int[] fun ={0,1,2,3,4,5,6};
System.arraycopy(fun,0,fun,3,3);
則結果為:{0,1,2,0,1,2,6};
(三)下面利用文件流將功能擴充一下
原來的代碼只能發送字符串的文字信息。如果要傳送文件呢?txt文件?zip文件之類的等等..
可能代碼擴充的不是太好,甚至當網絡不好的時候還會出現一些問題,但是先將補充好的代碼貼一下,后面如果有完善和修正的話再修改。
package bigbang; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConsumerCancelledException; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.ShutdownSignalException; import com.rabbitmq.client.Connection; import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.Arrays; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import com.rabbitmq.client.Channel; class RecvThread extends Thread{ RecvThread(String name){ super(name);//調用父類帶參數的構造方法 } public void run(){ final String QUEUE_NAME = "hello"; ConnectionFactory factory = new ConnectionFactory(); factory.setHost("115.159.181.204"); factory.setPort(5672); factory.setUsername("admin"); factory.setPassword("admin"); // 打開連接和創建頻道,與發送端一樣 Connection connection = null; try { connection = factory.newConnection(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Channel channel = null; try { channel = connection.createChannel(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 聲明隊列,主要為了防止消息接收者先運行此程序,隊列還不存在時創建隊列。 try { channel.queueDeclare(QUEUE_NAME, false, false, false, null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 創建隊列消費者 System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); // 指定消費隊列 // 指定消費隊列 QueueingConsumer consumer = new QueueingConsumer(channel); try { channel.basicConsume(QUEUE_NAME, true, consumer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while(true){ QueueingConsumer.Delivery delivery = null; try { delivery = consumer.nextDelivery(); } catch (ShutdownSignalException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ConsumerCancelledException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] message =delivery.getBody(); //String message = new (delivery.getBody()); try { OutputStream os=new FileOutputStream("C:/Users/cdmin/Desktop/121.zip",true); //byte[] buffer=delivery.getBody().length; try { os.write(message); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("file has been received! please scan your desktop!"); //System.out.println(" [x] Received '" + message + "'"); } } } public class zipzip { private final static String QUEUE_NAME = "hello"; private static Object message; public static void main(String[] args) throws java.io.IOException { /** * 創建連接連接到MabbitMQ */ // 設置MabbitMQ所在主機ip或者主機名 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("115.159.181.204"); factory.setPort(5672); System.out.println("input your username:"); Scanner un=new Scanner(System.in); String username=null; username=un.next(); System.out.println("input your password::"); Scanner up=new Scanner(System.in); String userpassword=null; userpassword=up.nextLine(); factory.setUsername(username); factory.setPassword(userpassword); // 創建一個連接 Connection connection = factory.newConnection(); // 創建一個頻道 Channel channel = connection.createChannel(); // 指定一個隊列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 發送的消息 //String message=null; //String str=null; InputStream is =new FileInputStream("C:/Users/cdmin/Desktop/12.zip"); byte[] buffer=new byte[8192]; byte[] message=new byte[8192]; int length=0; while(-1!=(length=is.read(buffer, 0, 8192))){ //System.out.println(length); System.arraycopy(buffer,0,message,0, length); //channel.basicPublish("", QUEUE_NAME, null, message); //for(int i=0;i<length;i++) message[i] = buffer[i]; // // str=new String(buffer,0,length); } is.close(); //System.out.println(buffer); //byte[] message = str.substring(0, str.length()-1).getBytes(); //System.out.println(message); //message=str; //Scanner s=new Scanner(System.in); //message=s.nextLine(); // 往隊列中發出一條消息 channel.basicPublish("", QUEUE_NAME, null, message); System.out.println(" [x] Sent '" + "12.zip"); // 關閉頻道和連接 channel.close(); connection.close(); RecvThread rthread= new RecvThread("test1"); rthread.start(); } }
運行結果如下:
比如傳送一個zip壓縮包------
控制台運行結果:
看一下桌面:
我要發送的是12.zip 桌面應該接收到一個12.zip的壓縮包 (這里為了區分我把接收到的壓縮包命名為121.zip)