java傳輸文件的簡單方法


假設現在已經打包了一個文件(1233444333),要將這個文件傳輸給另一方:

package file;
import java.io.*;

public class F_PasswordUnPassword {

    public static void main (String[] args)throws Exception {
        ByteArrayOutputStream arrOut=new ByteArrayOutputStream();
        DataOutputStream dataOut=new DataOutputStream(arrOut);
        //定義需要上傳的數據
        long data=1233444333;
        dataOut.writeLong(data);    //裝載數據到arrOut中
        
        //定義接受的數據
        byte [] receiveArr=arrOut.toByteArray();    
        //toByteArray()創建一個新分配的字節數組。 其大小是此輸出流的當前大小,緩沖區的有效內容已被復制到其中。 
        //將該輸出流的當前內容,作為字節數組。 
        ByteArrayInputStream arrin=new ByteArrayInputStream(receiveArr);
        DataInputStream dataIn=new DataInputStream(arrin);
        data=dataIn.readLong();    //讀出數據
        
        System.out.println("接受到的數據為:"+data);
        
    }

}

 

其中的上傳數據模塊和下載模塊可以單獨進行分裝后使用。

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM