Simple problem with Java file transfer program

I am trying to perform a simple file transfer from server to client.

It should look like this:

The client requests a file.

The server sends the file to the client.

Now in the code (below) (this is the only code I found, and it was hard to find) It sends me only the text file β€œgood” (and even this makes it only one line on the client). If I try to send any other type of file (e.g. image or rar file), it will be corrupted.

So, can someone please help me find some working code (in Java) that can send and receive all types of files or explain to me that the problem is with this code.

Server side:

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("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(); System.out.println("The requested file is : "+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("File transfered"); cs.close(); ss.close(); } } } 

Client side:

 class MyClient { 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",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("Enter the file name to transfer from server:"); DataInputStream dis=new DataInputStream(System.in); f=dis.readLine(); put.println(f); File f1=new File("c:\\output"); FileOutputStream fs=new FileOutputStream(f1); while((u=get.readLine())!=null) { byte jj[]=u.getBytes(); fs.write(jj); } fs.close(); System.out.println("File received"); s.close(); } } 

yesssssssssssssssssssssssssssssssssssssssssssssssssssssssss finally ...

These are changes

on the client side.

 class MyClient {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",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("Enter the file name to transfer from server:"); DataInputStream dis=new DataInputStream(System.in); f=dis.readLine(); put.println(f); File f1=new File("c:\\output"); 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("File received"); s.close(); } } 

Server side

 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("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(); System.out.println("The requested file is : "+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("File transfered"); cs.close(); ss.close(); } } } 

Many thanks to all of you ...

+6
source share
3 answers

In this case, you should not use a reader. The reader should work when you read / write character data (text), not binary. Use some kind of InputStream

http://download.oracle.com/javase/tutorial/i18n/text/stream.html

Update: In your server side after f.exist ():

 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("File transfered"); cs.close(); ss.close(); 
+4
source

The Reader / Writer class is for character stream only, Reader doc . You should use BufferedInputStream / BufferedOutputStream or something similar for raw / binary data.

+1
source

Yes, but InpustStreamReader is still a Reader that will try to convert base bytes to character data, not binary data. Since you are simply trying to send raw bytes of a file, do not use anything with a β€œreader” or β€œwriter” in the name β€” use InputStream and OutputStream (and their subclasses) instead on the server and client.

0
source

Source: https://habr.com/ru/post/888847/


All Articles