2013-03-29 35 views
0

我正在嘗試編寫簡單的「FTP」程序,但後來突然發生錯誤。所以這是一個帶客戶端和服務器的網絡,服務器存儲從客戶端上傳的文件,也可以從服務器上下載文件。但是,當我上傳文件它被保存在服務器目錄作爲一個空文件,有人會幫我找到代碼中的錯誤?Java中的文件傳輸無法正常工作

下面是客戶端

String nameOfFileToUp = fileFromFileChooser.getName(); 
System.out.println("fileChooserfile name= " + fileFromFileChooser.getName()); 
System.out.println("File path= " + fileFromFileChooser.getPath()); 
pw.println(nameOfFileToUp); 
File sendFile = new File(fileFromFileChooser.getPath()); 
FileInputStream fis = new FileInputStream(sendFile); 
int size =(int) fileFromFileChooser.length(); 
byte[] buffer = new byte[size+1]; 
int bytes = 0; 
while((bytes = fis.read(buffer)) != -1) 
{ 
    out.write(buffer,0,bytes); 
} 
fis.close(); 

其中PW是爲PrintWriter,

而且服務器

FileOutputStream fos = new FileOutputStream(f); 
DataOutputStream dops = new DataOutputStream(fos); 

while(done) 
{ 
    fc = in.readLine(); 
    if(fc == null) 
    { 
     done = false; 
    } 
    else 
    { 
     dops.writeChars(fc); 
    } 
} 
fos.close(); 

誰能幫助?請

回答

0

您需要刷新/關閉輸出流。另外,你的服務器不應該被「行」讀取,它應該是讀取字節(就像你的客戶端代碼一樣)。

+0

非常感謝!它現在有效 – Filip