2011-12-12 2 views
0

저는 간단한 http 클라이언트 서버를 가지고 있습니다. 여기서 클라이언트는 서버에서 다운로드 할 수 있습니다. 내 코드 클라이언트에서 파일을 다운로드 할 수 있습니다. 이것은 기본적으로 서버 폴더의 파일이 클라이언트로 성공적으로 전송되었음을 의미합니다. 내가하고 싶은 일은 콘솔에 다운로드 한 파일을 보여주는 것입니다. 내 질문은 어떻게 콘솔 밖으로받은 파일을 보여줍니다 내 코드를 수정할 수 있습니다. 콘솔에 다운로드 한 파일을 출력하십시오.

내 서버 :

public class Fileagent extends Thread 
     { 
      Socket client; 

      DataInputStream din; 
      DataOutputStream dout; 
      ServerSocket soc; 
      PrintWriter w; 
      BufferedReader r; 
public Fileagent(Socket soc) 
     { 
       try 
     { 
       client=soc;  
       din=new DataInputStream(client.getInputStream()); 
       dout=new DataOutputStream(client.getOutputStream()); 
       w = new PrintWriter(client.getOutputStream(), true); 
       r= new BufferedReader(new InputStreamReader(client.getInputStream())); 
       BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); 

      start(); 

     } .................. 





      public void upload() throws Exception 
     { 


      String file=din.readUTF(); 
      File f=new File(file); 
      System.out.println("\nThe client requested to download the file"+" "+f); 
      boolean exsists=check(f); 

      if (exsists==false){ 
       System.out.println("File not found in server"); 
       out.writeUTF("ERROR");//if not found sends an ERROR message 
      return; 
      } 
     else 
      { 
      dout.writeUTF("FOUND");//if found in server sends a FOUND message. 

      FileInputStream fin=new FileInputStream(f); 
      int ch; 
      //reads and sends the file 
     do 
     { 
      ch=fin.read(); 
      dout.writeUTF(String.valueOf(ch)); 
     } 
     while(ch!=-1); 
     fin.close(); 
      dout.writeUTF("File Receive Successfully"); 
     } 
     } 


     public Boolean check(File file){ 

     if(file.exists()) 
     { 
      return true; 
     } 
      else 
     { 

     return false; 
     } 
     } 

클라이언트 코드 다운로드 :

    public void download(Socket s) throws Exception{ 


     DataInputStream din=new DataInputStream(s.getInputStream()); 
      DataOutputStream dout=new DataOutputStream(s.getOutputStream()); 
     BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream())); 
      BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); 
      PrintWriter w = new PrintWriter(s.getOutputStream(), true); 
      String request; 
      System.out.print("Enter File Name :"); 



     request=con.readLine(); 
     dout.writeUTF(request); 
     String msg=din.readUTF(); 


     if(msg.compareTo("ERROR")==0) 
     { 
      System.out.println("File not found on Server ..."); 
     return; 
     } 
     else if(msg.compareTo("FOUND")==0)  
    { 
     System.out.println("Receiving File ..."); 
     File f=new File(request); 
     String option; 
     boolean exsists=check(f); 
     if(exsists==true) 
     { 
     String Option; 
      System.out.println("File Already Exists. Want to OverWrite (Y/N) ?"); 

     Option=con.readLine(); 
      if(Option=="N") 
      { 
     dout.flush(); 
      return; 
      } 

     } 


     FileOutputStream fileout=new FileOutputStream(f); 
     int ch; 
     String temp; 
    do 
    { 
     temp=din.readUTF(); 
     ch=Integer.parseInt(temp); 
     if(ch!=-1) 
    { 
    fileout.write(ch);  
    } 
    }while(ch!=-1); 
    fileout.close(); 
    System.out.println(din.readUTF()); 
    System.out.println("success"); 
    } 

답변

0

당신의 할 일에 클라이언트 코드의 루프, System.out.write을 (추가하는 동안) :

do { 
    temp=din.readUTF(); 
    ch=Integer.parseInt(temp); 

    if (ch!=-1) { 
     fileout.write(ch);  
     System.out.write(ch); 
    } 
} while(ch!=-1); 
관련 문제