Note that there are some explanatory texts on larger screens.

plurals
  1. POFile Transfer from Server to Client using java sockets. Error on Server side and File transferred to client is empty
    text
    copied!<p>I have a Client and Server, where in client enters the filename, that filename will be checked on server side under pre-defined path, if the file exists, it will be transferred to client side under a similar pre-defined path. I have two problems :</p> <p>1) In Server, i am not able to compare the file under given pre-defined path, as it is showing FileNotFoundException (no such file/dir).</p> <p>2) Even following the above exception, the file is transferred and it is empty.</p> <p>Here are my Client and Server:</p> <p>Client: </p> <pre><code>import java.io.*; import java.net.*; import java.util.*; public class ft2client { 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); } catch(Exception e) { System.exit(0); } 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); String str = "/home/user/"; FileOutputStream fs=new FileOutputStream(new File(str,f1.toString())); while((u=get.readLine())!=null) { byte jj[]=u.getBytes(); fs.write(jj); } fs.close(); System.out.println("File received"); s.close(); } } </code></pre> <p>Server:</p> <pre><code>import java.io.*; import java.net.*; import java.util.*; public class ft2server { 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 str = "/home/user/Desktop/"; String path = str + 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.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("File transfered"); cs.close(); ss.close(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload