简体   繁体   中英

Is it possible in android to transfer file over http using java socket program?

I'm creating an application to send a file from an android device to another device using http socket program. Here's the code, Server Side

try {
            ServerSocket servsock = new ServerSocket(4042);
            File myFile = new File(pathToFile);
            while (true) {
                Socket sock = servsock.accept();
                byte[] mybytearray = new byte[(int) myFile.length()];
                BufferedInputStream bis = new BufferedInputStream(
                        new FileInputStream(myFile));
                bis.read(mybytearray, 0, mybytearray.length);
                OutputStream os = sock.getOutputStream();
                os.write(mybytearray, 0, mybytearray.length);
                os.flush();
                bis.close();
                sock.close();

            }
        } catch (Exception e) {
            Toast.makeText(MyActivity.context,
                    "Failed to send file!", Toast.LENGTH_SHORT)
                    .show();
        }

And the client side,

try {
                Socket sock = new Socket(ipAddress, 4042);
                byte[] mybytearray = new byte[1024];
                InputStream is = sock.getInputStream();
                FileOutputStream fos = new FileOutputStream("/mnt/sdcard/"
                        +fileName);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                int bytesRead = is.read(mybytearray, 0, mybytearray.length);
                bos.write(mybytearray, 0, bytesRead);
                bos.close();
                sock.close();
            } catch (Exception e) {
                Toast.makeText(MyActivity.context,
                        "Failed to download the file!", Toast.LENGTH_LONG)
                        .show();
            }

Both these codes are in an AsyncTask class. But client side is always getting timed out. Is this the right way to transfer a file over Internet in android? If so, please tell me where I'm doing wrong. Thanks in advance.

EDIT : Here, the file needs to be send in Server side and Client is trying to download that file.

In the server side code:

byte[] mybytearray = new byte[(int) myFile.length()];

If myFile is the destination, and you have just created it (meaning the file does not yet exist), then myFile.length() would be 0. That means you are creating a buffer of size 0, reading 0 bytes.

Server needs to know how much data to read from the socket. A workaround could be that the android app first writes the length of the file, server reads it and creates a buffer according to that, and after that you write the complete file. Also, I suggest you move your .close() calls to a finally block.

Or, you could just use for example Apache FTP Client on Android and your favorite FTP server on the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM