2013-04-27 25 views
1

我正在寫一些代碼,其中使用套接字,但得到與大小一些文件發送從客戶端文件到服務器零

  1. 客戶端的命令行取文件名(文本文件)
  2. 客戶端的每個文件轉換爲字節數組,然後將這個數組發送到服務器。
  3. 服務器啓動一個新的線程,每個線程正在將某個字節數組轉換爲某個指定目錄中的新文件。

我的客戶端代碼

針對服務器端的客戶端

import java.io.*; 
import java.net.*; 

class Client 
{ 
    public static void main(String args[]) 
    { 
     try 
     { 
      int len=args.length; 
      if(len== 0) 
      { 
       System.out.println("Invalid Number of Arguements"); 
       throw new Exception(); 
      } 
      else 
      { 
       int i=0; 
       while(i<len) 
       {   
        Socket s=new Socket("localhost",2222); 
        File f=new File(args[i++]); 
        if(f.exists()) 
        { 
         OutputStream os=s.getOutputStream(); 
         FileInputStream fr=new FileInputStream(f); 
         s.setSendBufferSize(fr.available()); 
         byte data[]=new byte[fr.available()]; 
         fr.read(data); 
         os.write(data); 
         os.flush(); 
         os.close(); 
         System.out.println("Sending file : "+f.getName()+" with size "+data.length); 
         fr.close(); 
        } 
        else 
        { 
         System.out.println("File : "+args[i++]+" doesn't exist."); 
        } 

        s.close(); 
       } 
      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

import java.net.*; 
import java.io.*; 
import java.util.Date; 

public class Server 
{ 
    public static final String dir_to_store_files="c:\\myfiles\\"; 

    public static void main(String[] args) throws Exception 
    { 
     ServerSocket ss=new ServerSocket(2222); 
     ss.setReceiveBufferSize(51200); 
     ss.setReuseAddress(true); 

     Socket s=null; 
     while((s=ss.accept()) != null) 
     { 
      new Thread(new ServerThreads(s,dir_to_store_files)).start(); 
      s=null; 
     } 
     ss.close(); 
    } 
} 

class ServerThreads implements Runnable 
{ 
    private static int fileNounce=0; 
    Socket s; 
    String directory; 

    ServerThreads(Socket s,String directory) 
    { 
     this.s=s; 
     this.directory=directory; 
    } 

    public void run() 
    { 
     try 
     { 
      InputStream is=s.getInputStream(); 
      File f=new File(directory+"\\NewFile-"+fileNounce+".txt"); 
      while(f.exists()) 
      { 
        fileNounce++; 
        f=new File(directory+"\\NewFile "+fileNounce+".txt"); 
      } 

      fileNounce++; 
      f.createNewFile(); 

      FileOutputStream fos=new FileOutputStream(f); 
      byte data[]=new byte[is.available()]; 
      System.out.print(is.available()+ " bytes are received from "+s.getRemoteSocketAddress()); 
      is.read(data); 
      System.out.println("\t\tCreating file : "+f.getAbsolutePath()+" at : "+new Date()+" of size : "+data.length); 
      is.close(); 
      fos.write(data); 
      fos.flush(); 
      fos.close(); 
      s.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

的問題:服務器無法正確地獲取文件意味着有時新文件創建零大小(無數據)。例如,我已經檢查發送239 c文件使用 java clie nt * c 服務器正在接收隨機數量的文件(範圍在180-235) 即使它接收的一些文件大小也是零。

回答

2

fr.available()是不是你認爲它是....你想File.length(),以及讀取文件的循環字節發送給服務器...

是。可用()也是錯的..。考慮一個客戶端,看起來像:

byte[] buffer = new byte[4096]; 
int len = 0; 
while ((len = fr.read(buffer)) >= 0) { 
    os.write(buffer, 0, len); 
} 
+0

沒有,可用()是多少字節可以無阻塞地讀取的指示....即使如此,它是一個暗示。例如,來自Socket的InputStream()將有可用()== 0,直到客戶端實際發送數據爲止...... – rolfl 2013-04-27 20:23:59

+0

其實我想要的是將文件(通常是文本文件)從一個系統複製到另一個系統(其中將進行進一步的處理)。 所以我應該使用套接字或正常文件複製(性能明智)?? – 2013-04-29 14:22:48

+0

如果from和to的位置都在本地機器上,則使用Files.copy(...)(來自java.nio.file.Files),否則使用套接字 – rolfl 2013-04-29 14:25:00

相關問題