0

I'm creating a simple file transfer application. Which sends selected file from one device to another. It works fine if I know the file name to be received.

But if I don't know the file name then it is not writing the file in specified location using FileOutputStream.

Here is my code:

Sender:

        try {
        // create socket
        // TODO: the port should match the one in Client
        ServerSocket servsock = new ServerSocket(5005);
        while (true) {
            Log.i("************", "Waiting...");

            Socket sock = servsock.accept(); // blocks until connection opened
            Log.i("************", "Accepted connection : " + sock);

            // sendfile

            // TODO: put the source of the file
            File myFile = new File ("/mnt/sdcard/download/1.html");
            byte [] mybytearray  = new byte [(int)myFile.length()];
            Log.i("####### file length = ", String.valueOf(myFile.length()) );
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(mybytearray,0,mybytearray.length);
            OutputStream os = sock.getOutputStream();
            String text = "1.html";
            byte[] bytes = text.getBytes("UTF-8");
            os.write(bytes);
            Log.i("************", "Sending...");
            os.write(mybytearray,0,mybytearray.length);
            os.flush();
            sock.close();
        }
    } catch (IOException e) {
        Log.i("Io execption ", "e: " + e);
    }

In senders code, I've specified the file which I want to send to another device. here it is 1.html, using this line:

File myFile = new File ("/mnt/sdcard/download/1.html");

Now following is my receiver code:

try {
        int filesize=900000; // filesize temporary hardcoded

        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        // localhost for testing
        // TODO: server's IP address. Socket should match one above in server
        Socket sock = new Socket("192.168.43.139",5005);

        Log.i("************", "Connecting...");

        // receive file
        byte [] mybytearray  = new byte [filesize];
        InputStream is = sock.getInputStream();
        // TODO: Put where you want to save the file
                /* N.B.:
                 * * To view if the file transfer was successful:
                 *       * use `./adb shell`
                 *       * use the app: File Manager
                 *
                 * * If you downloaded to '/mnt/sdcard/download',
                 *   your download might not show up in 'Downloads'
                 *
                 * * You might not have '/mnt/sdcard/download' directory
                 *   if you have never downloaded anything on your iPhone
                 */




        FileOutputStream fos = new FileOutputStream("/mnt/sdcard/download/1.html");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;
        do {
            bytesRead =
                    is.read(mybytearray, current, (mybytearray.length-current));
            if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);

        bos.write(mybytearray, 0 , current);
        bos.flush();
        long end = System.currentTimeMillis();
        Log.i("** end-start = ", String.valueOf(end-start));
        bos.close();
        sock.close();
    } catch ( UnknownHostException e ) {
        Log.i("******* :( ", "UnknownHostException");
    } catch (IOException e){
        Log.i("Read has IOException", "e: " + e);
    }

Now received file is stored only if I set FileOutputStream to 1.html. Otherwise it is not saved.

So how can I make this process dynamic. I mean, I can save any file which is sent by the sender, without knowing its type of exact name?

This code is working fine if I provide file name to the receiver.

For reference, I'm following this tutorial or code.

Please help! and Thanks!

Kaushal28
  • 503
  • 1
  • 6
  • 18

0 Answers0