如何使用Socket

时间:2015-09-08 04:22:39

标签: java android sockets

如何使用Socket维护其格式将文件从服务器传输到Android Mobile。它可以是任何文件,如pdf,html,png,txt等。我想将此文件从Server推送到Android Mobile,但在Mobile端保存文件时我想知道来自Server的文件格式。那怎么可能呢?

private class ClientRxThread extends Thread {
        String dstAddress;
        int dstPort;

        ClientRxThread(String address, int port) {
            dstAddress = address;
            dstPort = port;
        }

        @Override
        public void run() {
            Socket socket = null;

            try {
                socket = new Socket(dstAddress, dstPort);
                String name = "test";
                File file = new File(
                        Environment.getExternalStorageDirectory(),
                        name);

                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

                byte[] bytes;
                FileOutputStream fos = null;
                try {
                    bytes = (byte[])ois.readObject();
                    fos = new FileOutputStream(file);
                    fos.write(bytes);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if(fos!=null){
                        fos.close();
                    }

                }

                socket.close();

                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,
                                "Finished",
                                Toast.LENGTH_LONG).show();
                    }});

            } catch (IOException e) {

                e.printStackTrace();

                final String eMsg = "Something wrong: " + e.getMessage();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,
                                eMsg,
                                Toast.LENGTH_LONG).show();
                    }});

            } finally {
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }

这是客户端 我想知道如何获取从服务器

发送的文件的文件格式
String name = "test";
                File file = new File(
                        Environment.getExternalStorageDirectory(),
                        name);

想知道如何查找从SERVER发送的文件的文件格式。来自服务器的文件可以是html,png或txt FILE

服务器端相关代码

public class FileTxThread extends Thread {
        Socket socket;

        FileTxThread(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            File file = new File(
                    Environment.getExternalStorageDirectory(),
                    "test.png");

            byte[] bytes = new byte[(int) file.length()];
            BufferedInputStream bis;
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(bytes, 0, bytes.length);

                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(bytes);
                oos.flush();

                socket.close();

                final String sentMsg = "File sent to: " + socket.getInetAddress();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,
                                sentMsg,
                                Toast.LENGTH_LONG).show();
                    }
                });

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }


File file = new File(
                        Environment.getExternalStorageDirectory(),
                        "test.png");

test.png可以有png,html,txt扩展名。那么有什么方法可以发送扩展名吗?

2 个答案:

答案 0 :(得分:2)

在服务器端也发送文件名。

File file = new File(Environment.getExternalStorageDirectory(),
                    "test.png");

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(file.getName().getBytes());
oos.flush();

//then send the file

在客户端首先接收该名称,然后将该文件作为第二个对象读取

byte[] fileNameBytes = (byte[])ois.readObject();
String name = new String(fileNameBytes);

//read the second object as file

答案 1 :(得分:1)

这可能会对您有所帮助,check this GitHub page