2014-01-25 6 views
1

다음은 사용자가 서버에서 파일을 요청할 수있는 서버 측 및 클라이언트 측의 코드입니다. 서버가 파일을 보냅니다.LAN을 통해 서버에서 클라이언트로 파일을 전송할 수 없습니다.

  • 서버 측이 빈 파일을 보냅니다

    두 가지 문제가있다. 로컬 영역 네트워크에서 코드를 실행하려고 할 때

  • 는 IOException이

단절이 빈 파일을 보내는 이유는 이해가 안을주고, 도와주세요.

서버 측 코드 :

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package ftpserverclient.FileClientServer; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

/** 
* 
* @author Arnab 
*/ 
public class FileServer { 

public static void main(String args[])throws IOException 
     { 
      ServerSocket ss=null; 
      try 
      { 
       ss=new ServerSocket(8085); 
      } 
      catch(IOException e) 
      { 
       System.out.println("couldn't listen"); 
       System.exit(0); 
      } 
      Socket cs=null; 
      try 
      { 
       cs=ss.accept(); 
       System.out.println("Connection established"+cs); 
      } 
      catch(Exception e) 
      { 
       System.out.println("Accept failed"); 
       System.exit(1); 
      } 
      PrintWriter put=new PrintWriter(cs.getOutputStream(),true); 
      BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); 
      String s=st.readLine(); 


      String path = s ; 
      System.out.println("The requested file is path: "+path); 
      System.out.println("The requested file is : "+s); 
      File f=new File(path); 
      if(f.isFile()) 
      { 
       BufferedReader d=new BufferedReader(new FileReader(f)); 
       String line; 
       while((line=d.readLine())!=null) 
       { 
        put.write(line); 
        put.flush(); 
       } 
       d.close(); 
       System.out.println("File transfered"); 
       cs.close(); 
       ss.close(); 
      } 
     } 

} 

클라이언트 측 코드 :

package ftpserverclient.FileClientServer; 

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

/** 
* 
* @author Arnab 
*/ 
public class FileClient { 

public static void main(String srgs[])throws IOException 
{ 
    Socket s=null; 
    BufferedReader get=null; 
    PrintWriter put=null; 
    try 
    { 
     s=new Socket("127.0.0.1",8085); 
     get=new BufferedReader(new InputStreamReader(s.getInputStream())); 
     put=new PrintWriter(s.getOutputStream(),true); 

     String u,f; 
     System.out.println("Enter the file name to transfer from server:"); 
     DataInputStream dis=new DataInputStream(System.in); 
     f=dis.readLine(); 
     put.println(f); 

     File f1=new File(f); 



     FileOutputStream fs=new FileOutputStream(new File("C:\\PictureDestination\\a.jpg")); 


     while((u=get.readLine())!=null) 
     { 
      System.out.println(u); 
      byte jj[]=u.getBytes(); 
      fs.write(jj); 
     } 
     fs.close(); 
     System.out.println("File received"); 
     s.close(); 
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.exit(0); 
    } 
}  

} 

답변

0

클라이언트 측에서, 당신은 서버에 파일 이름을 전송하는 동안 put.println(f);put.fflush();를 작성 잊어 버렸습니다.

+0

나는'put.flush()'를 시도했지만 여전히 운이 없다. – ARNAB2012

0

마침내 나는이 것이 작동하는 것을 발견했다.

여기가 변경되었습니다.

while((u=get.read(jj,0,1024))!=-1) { fs.write(jj,0,u); } fs.close();

하지만 한 문제에 String f; int u;-String u,f;while((u=get.readLine())!=null) { System.out.println(u); byte jj[]=u.getBytes(); fs.write(jj); } 변화는 여전히 존재합니다. 그것은 LAN에서 작동하지 않습니다.

+0

어떻게 서버 IP 주소를 찾았습니까? –

+0

서버 컴퓨터의 cmd에서'ipconfig' 명령을 실행했고 컴퓨터의 IP 주소가 – ARNAB2012

+0

인데 서버의 실제 IP 주소는'192.168.0.2'이고 IP가 192.168.0.3 인 컴퓨터에서 클라이언트 측 프로그램을 실행했습니다 '(s = new Socket ("127.0.0.1", 8085);'에서's = new Socket ("192.168.0.2", 8085);'로 클라이언트 쪽 코드를 변경하면 오류가 발생합니다. 즉 서버 사이드 코드는 ioexception 즉,'could not listen'을 출력합니다. – ARNAB2012

관련 문제