2011-08-14 34 views
0

對不起,但我改變了這個問題。通過插槽傳輸文件

在此code如果文件在第一次發送(無論我發送的數字文件),代碼工作正常。但是當我把FileSender放在一個循環中逐一發送文件時,在第一次傳輸之後,在接收器端收到的數據是任意的(如果在調試過程中檢查過),它甚至不會接收文件。這是我所做的改變,它不起作用。 FileSender.java

import java.io.OutputStream; 
import java.io.File; 
import java.net.Socket; 
public class FileSender { 
public void main(Socket socket,String[] args) { 
try { 

    OutputStream os  = socket.getOutputStream(); 
    int cnt_files = args.length; 

    // How many files? 
    ByteStream.toStream(os, cnt_files); 

    for (int cur_file=0; cur_file<cnt_files; cur_file++) { 
    ByteStream.toStream(os, new File(args[cur_file]).getName()); 
    ByteStream.toStream(os, new File(args[cur_file])); 
    } 
} 
catch (Exception ex) { 
    ex.printStackTrace(); 
} 
} 
} 

字串[] args包含要被髮送的文件的路徑。

FileReceiver.java

import java.io.File; 
import java.io.InputStream; 
import java.net.Socket; 

public class FileReceiver { 

public void main(Socket socket,String arg) { 
try { 
    InputStream in = socket.getInputStream(); 

    int nof_files = ByteStream.toInt(in); 
System.out.println("reach 1  "+ nof_files); 
    for (int cur_file=0;cur_file < nof_files; cur_file++) { 
    String file_name = ByteStream.toString(in); 

    File file=new File(arg+file_name); 
    System.out.println("Received path is : "+file); 
    ByteStream.toFile(in, file); 
    } 

} 
catch (java.lang.Exception ex) { 
    ex.printStackTrace(System.out); 
} 
} 

} 

ARG包含這些文件將被存儲的路徑。

我希望我打電話主要函數提到每當我想傳輸文件。基本上我想傳輸多個可以包含目錄的文件。爲此我寫了下面的代碼。

ServerFile.java

import java.io.*; 
import java.net.*; 
public class ClientFile implements Runnable{ 
Socket clientsocket; 
public void run() { 
    try 
    { 
     clientsocket = new Socket("219.64.189.14",6789); 
    // Some code 
    copy(outtoserver,infromserver, files);  // files contains the path of files to be transferred. 
    // Some code 
     clientsocket.close(); 
    } 
    catch(Exception e2) 
    { 
      System.out.println("ClientFile "+String.valueOf(e2) + "\n"); 
    } 
} 
public void copy(DataOutputStream outtoserver,BufferedReader infromserver,String[] files) 
{ 
    try 
    { 
     FileSender fs = new FileSender(); 
     int totalfiles=0; 
     int r=0; 
     File oldfile; 
     outtoserver.write(files.length); 
     String chk; 
     while(totalfiles<files.length) 
     { 

      oldfile = new File(files[totalfiles]); 
      if(oldfile.isDirectory()) 
      { 
       outtoserver.writeBytes("folder\n"); 
       File folder1[] = oldfile.listFiles(); 
       String[] folder = new String[folder1.length]; 
       int count=0; 
       for(File name : folder1) 
       { 
        folder[count] = name + ""; 
        System.out.println(folder[count]); 
        count++; 
       } 
       outtoserver.writeBytes(oldfile.getName()+"\n"); 
       fs.main(clientsocket, folder); 

      } 
      else if(oldfile.isFile()) 
      { 
       outtoserver.writeBytes("file\n"); 
     chk = infromserver.readLine(); 
       if(chk.equals("send")) 
       { 
        outtoserver.writeBytes(oldfile.getName()+"\n"); 
        String[] folder = new String[]{oldfile.getAbsolutePath()}; 
        fs.main(clientsocket, folder); 
       } 
       totalfiles++; 
       outtoserver.flush(); 

      } 
     } 
    } 
    catch(Exception e) 
    { 
     System.out.println("ClientFile -->> "+e.toString()); 
    } 
} 
} 

ClientFile.java

import java.io.*; 
import java.net.*; 
import javax.swing.*; 
class ServerFile implements Runnable { 
Socket conn; 
public ServerFile(Socket a) 
{ 
    conn = a; 
} 
public void run() { 
    File file1; 
    String clientsen=""; 
    try 
    { // Some code 
     copy(outtoclient,infromclient,file1.getAbsolutePath());  //file1 is the directory to which the file has to stored.  
    // some code 
    }  
    catch(Exception e0) 
    { 
     System.out.println("ServerFile "+String.valueOf(e0)+"\n"+e0.getCause()); 
    } 
}//end main 
public void copy(DataOutputStream outtoclient,BufferedReader infromclient,String basepath) 
{ 
    try 
    { 
     FileReceiver fr = new FileReceiver(); 
     int totfiles = infromclient.read(); 
     int tot=0; 
     File file; 
     String path = null,chk; 
     while(tot<totfiles) 
     { 
      chk = infromclient.readLine(); 
      if(chk.equals("file")) 
      { 
       outtoclient.writeBytes("send\n"); 
       path = infromclient.readLine(); 
       path = basepath+File.separator+path; 
       file=new File(path); 
       fr.main(conn, basepath+File.separator); 
      } 
      else if(chk.equals("folder")) 
      { 
       String name = infromclient.readLine(); 
       name = basepath+File.separator+name; 
       new File(name).mkdir(); 
       fr.main(conn, name+File.separator); 
      } 
      tot++; 
     } 
    } 
    catch(Exception e) 
    { 
     System.out.println("Server file: "+e.toString()); 
    } 
} 

}//end class 

糾正我,通過各種手段,如果我錯了。

任何幫助表示讚賞。

+0

不知道什麼這些類呢,還是什麼的另一端的模樣。但是我會猜測你使用的代碼是這樣的? http://www.adp-gmbh.ch/blog/2004/november/15.html(對不起,沒有注意到你的文章中的鏈接) –

+0

是的,這就是我提到的。 – pankaj

+0

什麼是'cnt_files'?您的循環似乎重複發送相同的文件。如果你可以顯示* complete * main方法,那真的會有所幫助。 –

回答

1

它看起來像你試圖設置文件的數量爲一個(ByteStream.toStream(os, 1);),但然後你發送所有的文件(從0到cnt_files-1,在內部循環),然後args [0]的東西嘗試使用args []中的下一個文件。有這個問題開始,因爲我認爲你正在試圖做的是沿着線的東西更多:

for(int i =0; i<n;i++){ 
    ByteStream.toStream(os, 1);//cnt_files); 
    ByteStream.toStream(os, args[i]); 
    ByteStream.toStream(os, new File(args[i])); 
} 

這仍然無法工作,雖然,因爲另一端的FileReceiver有這:

int nof_files = ByteStream.toInt(in); 

所以FileReceiver的第一件事就是看看有多少文件。它只會看到1,其循環將結束,其他文件將不會被讀取。

如果「一一」你的意思是每個連接一個文件,那麼你想要的東西是這樣的:

public static void main(String[] args) { 

    try { 
     for(int i =0; i<args.length;i++){ 
      Socket socket = new Socket(host, port); 
      OutputStream os = socket.getOutputStream(); 

      ByteStream.toStream(os, 1);//cnt_files); 
      ByteStream.toStream(os, args[i]); 
      ByteStream.toStream(os, new File(args[i])); 

      os.close(); 
      socket.close(); 
     } 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

是的,你是對的。但是我真正想要做的是不要一次又一次地建立連接,但一旦建立連接,我就可以一次又一次地發送文件,而不需要終止FileSender代碼。 – pankaj

+0

它看起來像原始代碼(來自adp-gmbh.ch鏈接)通過一個連接發送所有文件。 –