【问题标题】:Android send large files via socketAndroid通过socket发送大文件
【发布时间】:2015-10-04 19:52:55
【问题描述】:

目前我正在尝试为我的 Android 设备创建一个小套接字应用程序。

我想通过套接字连接将大文件 (~500MB) 发送到我的笔记本电脑/PC 或其他任何东西。我在我的 Android 设备上使用套接字客户端连接到我的 PC 上的套接字服务器,但是,当我尝试发送测试字段 (~460MB) 时,我的应用程序崩溃并显示:

"Throwing OutOfMemoryError"分配441616290字节失败 分配 4194304 个可用字节和 90MB 直到 OOM""

我猜我的客户无法处理这个文件大小。所以我的问题是:有没有办法通过 TCP 套接字连接来处理这么大的文件?我的代码适用于小文件(例如 5MB),但适用于较大的文件。

这是我目前所拥有的:

在我的安卓设备上运行的客户端:

private class Connecting extends AsyncTask<String, Integer, String>
{
    @Override
    protected String doInBackground(String... serverAdd) 
    {
        String filePath = "Path to file";

        File sdFile = new File(filePath);

        try {

            client = new Socket("ip", "port");

            outputStream = client.getOutputStream();                
            byte[] buffer = new byte[1024];
            FileInputStream in = new FileInputStream(sdFile);
            int rBytes;
            while((rBytes = in.read(buffer, 0, 1024)) != -1)
            {
              outputStream.write(buffer, 0, rBytes);
            }

            outputStream.flush();
            outputStream.close();
            client.close(); 


        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

服务器端:

public class Main {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static FileOutputStream fileOutputStream;
private static BufferedOutputStream bufferedOutputStream;
private static int filesize = 10000000;
private static int bytesRead;
private static int current = 0;

public static void main(String[] args) throws IOException {

    serverSocket = new ServerSocket(10898);

    System.out.println("Server started. Listening to the port 10898");

    clientSocket = serverSocket.accept();

    byte[] mybytearray = new byte[filesize];   

    inputStream = clientSocket.getInputStream();
    fileOutputStream = new FileOutputStream("E:\\output.zip");
    bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

    System.out.println("Receiving...");

    bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
    current = bytesRead;

    do {
        bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
        if (bytesRead >= 0) {
            current += bytesRead;
        }
    } while (bytesRead > -1);


    bufferedOutputStream.write(mybytearray, 0, current);
    bufferedOutputStream.flush();
    bufferedOutputStream.close();
    inputStream.close();
    clientSocket.close();
    serverSocket.close();

    System.out.println("Sever recieved the file");

}
}

问候

[编辑]:客户端代码。

【问题讨论】:

  • 您需要将文件分成更小的文件,通常称为块。这是一个示例,在 C 中:stackoverflow.com/questions/1628587/…
  • 不要一次读入整个文件,分小块读。
  • 好的,谢谢你提供的信息,我需要一些时间来研究这段代码,因为我不熟悉 c。
  • 只需使用与发送相同的代码来接收。在将其写入文件之前,无需将整个输入读入内存。

标签: android sockets tcp


【解决方案1】:

这里是关于如何将文件分块的 Java 示例代码。您可以将此应用于您的 TCP/IP 客户端-服务器应用程序。更多关于分块的信息可在此处 (Java - Read file by chunks?) 获得,我从中提取了样本。

char[] myBuffer = new char[1024];
int bytesRead = 0;
BufferedReader in = new BufferedReader(new FileReader("foo.txt"));
while ((bytesRead = in.read(myBuffer, 0, 1024)) != -1)
{
    ...
}

【讨论】:

  • 好的,这帮助我找到了我的错误,它正在将文件发送到我的服务器。对你们所有人来说,坦克很多:)
猜你喜欢
  • 2021-07-09
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 2012-06-26
相关资源
最近更新 更多