如何使用Socket将文件从服务器传输到安卓手机

3

如何通过Socket将文件从服务器传输到Android手机并保持其格式。可以是任何类型的文件,例如pdf、html、png、txt等。我想要把这个文件从服务器推送到Android手机上,但在手机端保存文件时,我想知道来自服务器的文件格式。那么应该怎么做呢?

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);

想要了解如何查找服务器发送的文件格式。来自服务器的文件可能是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 等扩展名。那么,有没有办法发送扩展名呢?


我不太了解套接字,所以我在问。 - dhaval gogri
1
你的问题并没有明确的问题,SO不会为你编写代码。你必须向我们展示一些你所做的努力。另外,我还没有给你点赞但是。(我正在帮助你提出正确的问题) - Emil
耶..你来了..你刚刚发了一些代码,请告诉我们你得到了什么错误? - Emil
你尝试过使用 File.getName() 吗? - Emil
我正在从服务器发送文件。但是该文件的扩展名可以是任何扩展名,例如test.html、test.png、test.txt。那么如何使用Socket获取带有扩展名的文件名?怎么做? - dhaval gogri
显示剩余7条评论
2个回答

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

我现在正在尝试,5分钟后会回复您。 - dhaval gogri
我有一个疑问。在客户端,我怎么知道我是发送文件名还是实际文件? - dhaval gogri
你可以按照以下方式定义自己的协议:首先发送/接收文件名,然后是文件内容。 - greenapps
如何定义协议? - dhaval gogri
已经通过我所说的定义了:先发送文件名。 - greenapps

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接