无法通过Android中的Wi-Fi热点传输更大尺寸的文件

时间:2016-12-13 10:27:23

标签: android hotspot

我想在两个Android设备之间使用Wi-Fi热点IP地址和MAC地址在Socket连接上传输文件。我能够传输小于1 KB大小但无法发送其他扩展文件的文本文件使用插座更大的尺寸。以下是发件人方的代码: -

            Socket socket = null;
            File file = new File(
                    Environment.getExternalStorageDirectory(),
                    "test.mp3");

            byte[] bytes = new byte[(int) file.length()];
            BufferedInputStream bis;
            try {
                socket = new Socket(dstAddress, dstPort);
                bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(bytes, 0, bytes.length);
                OutputStream os = socket.getOutputStream();
                os.write(bytes, 0, bytes.length);
                os.flush();
                if (socket != null) {
                    socket.close();
                }

                final String sentMsg = "File Sent.....";
                ((Activity)context_con).runOnUiThread(new Runnable() {

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


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

这是Receiver End的代码: -

           try {
            File file = new File(
                    Environment.getExternalStorageDirectory(),
                    "test.mp3");

            byte[] bytes = new byte[1024];
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int bytesRead = is.read(bytes, 0, bytes.length);
            bos.write(bytes, 0, bytesRead);
            bos.close();
            socket.close();

            final String sentMsg = "File Received...";
            Main.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(Main.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();
            }
        }

我想传输更大尺寸的文件,如mp3文件,但它只在接收器端创建1Kb大小的文件,而不是2.1 MB的确切大小。请帮助我在这个实现中出错。

1 个答案:

答案 0 :(得分:0)

把它放在一起但没有测试过。这应该给出一些提示

 //Server side snippet
 public void server(int port) throws IOException {
    try (ServerSocket serverSocket = new ServerSocket(port); Socket socket = serverSocket.accept()) {
        try (InputStream in = socket.getInputStream(); OutputStream out = new FileOutputStream("test.mp3")) {
            byte[] bytes = new byte[2 * 1024];

            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
        }
    }
}

 //client side
 public static void client(String dstAddress, int dstPort) throws IOException { 
    try (Socket socket = new Socket(dstAddress, dstPort)) {
        File file = new File(Environment.getExternalStorageDirectory(), "test.mp3");

        // Get the size of the file
        long length = file.length();
        if (length > 0) {
            byte[] bytes = new byte[2 * 1024];
            InputStream in = new FileInputStream(file);
            OutputStream out = socket.getOutputStream();

            int count;
            while ((count = in.read(bytes)) > 0) {
                out.write(bytes, 0, count);
            }
            out.close();
            in.close();
        }
    }
}

您可以选择使用try-catch包装资源,或者您可以选择不这样做。请考虑相应地调整缓冲区大小。考虑this

请尝试。