1

I am trying to read the binary files using streams and sockets in java but was not able to open the file after reading and copying in the destination.

Please have a look at the code and suggest me the possible solution. I am sending a file from admin side to server.so,that the file will be saved on file system.

Please do not mark as duplicate as i was not able to find the solution anywhere Server side program

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class N {
public static void main(String[] args) throws IOException {
    ServerSocket servsock = new ServerSocket(4567);
    File myFile = new File("C:/Users/rp00491429/Downloads/greatdeepak.mp4");
    System.out.println("Waiting for connection");  
    Socket sock = servsock.accept();
      System.out.println("connected");
      byte[] mybytearray = new byte[(int) myFile.length()];
      FileInputStream fis=(FileInputStream)sock.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray);
      for(byte e:mybytearray) {
          System.out.print(e);
      }
      System.out.println(mybytearray.length);
      File f=new File("C:/Users/rp00491429/Downloads/dfgd.mp4");
      FileOutputStream os = new FileOutputStream(f);
      BufferedOutputStream bos=new BufferedOutputStream(os);
      bos.write(mybytearray);
      os.flush();
      sock.close();
    }}

Client side program

    public static void main(String[] ar) {

    try {
        Socket socket=new Socket("localhost",4567);
        File file=new 
         File("C:\\Users\\rp00491429\\Downloads\\greatdeepak.mp4");
        byte b[]=new byte[(int)file.length()];
        FileInputStream fis=new FileInputStream(file);
        fis.read(b);
        FileOutputStream fos=(FileOutputStream) socket.getOutputStream();
        fos.write(b);
    } catch (Exception e) {

    }

I was able to store the file in the path but not able to open. please have a look at the screenshotdfgd.mp4 in file system but not able to open

Ramana
  • 31
  • 7
  • `Files.copy(sock.getInputStream(), Paths.get("C:/Users/rp00491429/Downloads/dfgd.mp4"));` – Joop Eggen Jan 08 '18 at 12:29
  • Thank you joop,please suggest where should i incude this line – Ramana Jan 08 '18 at 13:24
  • (Just wanted to respond as you mentioned the question not being a duplicate.) The above would copy the entire socket's input stream to a .mp4 file. So should replace all between `servsocket.accept();` and `sock.close();` – Joop Eggen Jan 08 '18 at 13:39
  • File was not opening by media player saying codec issue.Can anyone suggest on how to solve the issue? – Ramana Jan 09 '18 at 06:21
  • Maybe sent by HTTP protocol; with header lines. Look into the file with what it begins. Ask an other question should you need further help. – Joop Eggen Jan 09 '18 at 08:32
  • @Joop ,Iam using socket programming,not sent by http protocol,kindly have a look at the code.text files are working fine but the problem is with binary files which are not even opening. – Ramana Jan 09 '18 at 11:01

0 Answers0