title: Simple Java file transfer program problem
body: <p>I am trying to do a simple file transfer from server to client.</p>
  <p>It needs to go like this:</p>
  <blockquote>
  <p>Client asks for file.</p>
  <p>Server sends the file to client.</p>
  </blockquote>
  <p>Now in the code (down below) (this is the only code I found and it was hard to find)
  It sends me only a text file &quot;good&quot; (and even that only makes it to one line in the client).
  If i try to send any other file type (like image or rar file) it gets it corrupted.</p>
  <p>So, could some one please help me to find some working code (in Java) that can send and receive all types of files, or explain to me what the problem with this code is.</p>
  <p>Server side:</p>
  <pre><code>public class FileServer {   
      public static void main(String args[])throws IOException
      { 
          ServerSocket ss=null;
          try
          {  
              ss=new ServerSocket(8081);
          }
          catch(IOException e)
          { 
              System.out.println(&quot;couldn't listen&quot;);
              System.exit(0);
          }
          Socket cs=null;
          try
          { 
              cs=ss.accept();
              System.out.println(&quot;Connection established&quot;+cs);
          }
          catch(Exception e)
          { 
              System.out.println(&quot;Accept failed&quot;);
              System.exit(1);
          } 
          PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
          BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
          String s=st.readLine();
          System.out.println(&quot;The requested file is : &quot;+s);
          File f=new File(s);
          if(f.exists())
          { 
              BufferedReader d=new BufferedReader(new FileReader(s));
              String line;
              while((line=d.readLine())!=null)
              {
                  put.write(line);
                  put.flush();
              }
              d.close();
              System.out.println(&quot;File transfered&quot;);
              cs.close();
              ss.close();
          }  
      }  
  }
  </code></pre>
  <p>Client Side:</p>
  <pre><code>class MyClient {
      public static void main(String srgs[])throws IOException
      {
          Socket s=null;
          BufferedReader get=null;
          PrintWriter put=null;
          try
          { 
              s=new Socket(&quot;127.0.0.1&quot;,8081);
              get=new BufferedReader(new InputStreamReader(s.getInputStream()));
              put=new PrintWriter(s.getOutputStream(),true);
          }  
          catch(Exception e)
          {
              System.exit(0);
          }
          String u,f;
          System.out.println(&quot;Enter the file name to transfer from server:&quot;);
          DataInputStream dis=new DataInputStream(System.in);
          f=dis.readLine();
          put.println(f);
          File f1=new File(&quot;c:\\output&quot;);
          FileOutputStream  fs=new FileOutputStream(f1);
          while((u=get.readLine())!=null)
          { 
              byte jj[]=u.getBytes();
              fs.write(jj);
          }
          fs.close();
          System.out.println(&quot;File received&quot;);
          s.close();
      }
  }
  </code></pre>
  <p><strong>yesssssssssssssssssssssssssssssssssssssssssssssssssss
  at last...</strong></p>
  <p>This is the changes</p>
  <p>client side.</p>
  <pre><code>class MyClient {public static void main(String srgs[])throws IOException
  {
      Socket s=null;
      BufferedReader get=null;
      PrintWriter put=null;
      try
      { 
          s=new Socket(&quot;127.0.0.1&quot;,8081);
          get=new BufferedReader(new InputStreamReader(s.getInputStream()));
          put=new PrintWriter(s.getOutputStream(),true);        
      }  
      catch(Exception e)
      {
          System.exit(0);
      }
      InputStreamReader get2=new InputStreamReader(s.getInputStream());
      String u,f;
      System.out.println(&quot;Enter the file name to transfer from server:&quot;);
      DataInputStream dis=new DataInputStream(System.in);
      f=dis.readLine();
      put.println(f);
      File f1=new File(&quot;c:\\output&quot;);
      FileOutputStream  fs=new FileOutputStream(f1);
  
      BufferedInputStream d=new BufferedInputStream(s.getInputStream());
      BufferedOutputStream outStream = new BufferedOutputStream(new             FileOutputStream(f1));
      byte buffer[] = new byte[1024];
      int read;
      while((read = d.read(buffer))!=-1)
      {
          outStream.write(buffer, 0, read);
          outStream.flush();
      }
  
      //while((u=get.readLine())!=null)
      // { 
      //    byte jj[]=u.getBytes();
      //    fs.write(jj);
      //} 
      fs.close();
      System.out.println(&quot;File received&quot;);
      s.close();
      }
  }
  </code></pre>
  <p>Server Side.</p>
  <pre><code>public class FileServer {   
      public static void main(String args[])throws IOException
      { 
          ServerSocket ss=null;
          try
          {  
              ss=new ServerSocket(8081);
          }
          catch(IOException e)
          { 
              System.out.println(&quot;couldn't listen&quot;);
              System.exit(0);
          }
          Socket cs=null;
          try
          { 
              cs=ss.accept();
              System.out.println(&quot;Connection established&quot;+cs);
          }
          catch(Exception e)
          { 
              System.out.println(&quot;Accept failed&quot;);
              System.exit(1);
          } 
          PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
          BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
          String s=st.readLine();
          System.out.println(&quot;The requested file is : &quot;+s);
          File f=new File(s);
          if(f.exists())
          { 
              BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
              BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
              byte buffer[] = new byte[1024];
              int read;
              while((read = d.read(buffer))!=-1)
              {
                  outStream.write(buffer, 0, read);
                  outStream.flush();
              }
              d.close();
              System.out.println(&quot;File transfered&quot;);
              cs.close();
              ss.close();
          }  
      }  
  }
  </code></pre>
  <p>Thanks a lot to all of you...</p>