繁体   English   中英

如何使用Socket将文件从服务器传输到Android Mobile

[英]How to transfer file from Server to Android Mobile using Socket

如何使用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();
                    }
                }
            }
        }
    }

这是客户端我想知道如何获取从Server发送的文件的文件格式

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

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

服务器端相关代码

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扩展名。 那么有什么方法可以发送扩展名吗?

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

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

这可能会对您有所帮助, 请查看此GitHub页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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