2013-07-03 30 views
0

我正在編寫服務器和客戶端文件傳輸模塊,我想知道是否可以將文件信息與文件本身一起發送。特別需要文件名和服務器上發送給客戶端的文件的文件夾結構。例如,如果我在服務器上有c:\ abc \ efg \ a.txt,我想要。\ abc \ efg \ a.txt在客戶端上。如何將文件信息與文件一起發送?

這是我使用的代碼:

服務器端發送文件:

Socket clientSocket=new Socket("Some IP",12345); 

    OutputStream out=clientSocket.getOutputStream(); 

    FileInputStream fis=new FileInputStream(file); 
    int x=0; 
    byte[] b = new byte[4194304]; 
    while(true){ 
     x=fis.read(b); 
     if(x==-1)break; 
     out.write(b); 
    } 
    out.close(); 

客戶端偵聽:

try { 
     ServerSocket ss=new ServerSocket(12345); 
     while(true){ 
      final Socket client = ss.accept(); 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try{ 
         InputStream in = client.getInputStream(); 
         FileOutputStream fos = new FileOutputStream("rec.txt"); 
         int x=0; 
         byte[] b = new byte[4194304]; 
         while(true){ 
          x=in.read(b); 
          if(x==-1)break; 
          fos.write(b); 
         } 
         fos.close(); 
        } 
        catch(Exception e){ 
        } 
       } 
      }).start(); 
     } 

    } catch (Exception e) { 
    } 
+0

當然是可能的,但你需要創建一個它更復雜的協議。或者你可以使用已經做過類似的事情,如HTTP或FTP的許多服務之一。 – biziclop

+0

謝謝,但我正在尋找一些本地支持,我知道所有這些選擇,但他們是我的最後選擇。 – armin

+0

沒有這種本地支持。除非你使用網絡文件系統,當然,但這不是這種情況。 – fge

回答

1

您需要發送文件路徑,然後是文件名,使用64字節的緩衝區來完成該操作,一旦獲得了讀取文件內容的路徑和名稱。

例如:

// Server 
    try 
    { 
     Socket clientSocket=new Socket("Some IP",12345); 
     OutputStream out=clientSocket.getOutputStream(); 
     FileInputStream fis=new FileInputStream(file); 
     byte[] info = new byte[64]; 
     int len = file.getPath().length(); 
     info = file.getPath().getBytes(); 
     for (int i=len; i < 64; i++) info[i]=0x00; 
     out.write(info, 0, 64); 
     len = file.getName().length(); 
     info = file.getName().getBytes(); 
     for (int i=len; i < 64; i++) info[i]=0x00; 
     out.write(info, 0, 64); 

     int x; 
     byte[] b = new byte[4194304]; 
     while((x=fis.read(b)) > 0) 
     { 
     out.write(b, 0, x); 
     } 
     out.close(); 
     fis.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    // Client 
    try 
    { 
     InputStream in = client.getInputStream(); 
     FileOutputStream fos = new FileOutputStream("rec.txt"); 
     byte[] path = new byte[64]; 
     in.read(path, 0, 64); 
     byte[] name = new byte[64]; 
     in.read(name, 0, 64); 

     int x=0; 
     byte[] b = new byte[4194304]; 
     while((x = in.read(b)) > 0) 
     { 
      fos.write(b, 0, x); 
     } 
     fos.close(); 
    } 
    catch(Exception e) 
    { 
    e.printStackTrace(); 
    } 
1

首先發送一個報頭與你需要的所有信息,然後分隔符(例如0)然後是文件內容。服務器端讀取標題直到分隔符,然後是內容。

+0

這還需要您首先發送標題/內容的長度。 – biziclop

1

我以前做過這個。我在服務器上使用DataOutputStream,在客戶端使用DataInputStream。

我用這個協議:
1.發送文件名.writeUTF(fileName);
2.發送文件大小.writeLong(fileSize);
3.發送文件.writeBytes(byteArray); //這是在for循環中完成的,因爲文件大小可能太大而無法立即存入內存。服務器和客戶端大小都將使用fileSize來確定何時停止。

客戶端將使用相同的協議,而不是「寫入」,它將被「讀取」。

0

郵編
文件
2.有關文件的信息
發送)

相關問題