gpt4 book ai didi

android - 将android中的大文件发布到服务器

转载 作者:行者123 更新时间:2023-12-03 11:55:43 25 4
gpt4 key购买 nike

我必须从 Android 应用程序向服务器发送大于 10MB 的大文件。它为我提供了大文件的OutOfMemory 异常。我已经尝试了很多解决方案,但没有任何效果。这是我的代码,适用于小文件。

        HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(fileName);

if (!sourceFile.isFile()) {
Log.e("clusterFileUploaded", fileName);
}

try {

FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(purl);

conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("clusterFile", fileName);

dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name='clusterFile';filename="+ fileName + lineEnd);

dos.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
int serverResponseCode = 0;
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
dos.flush();
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

DataInputStream is = new DataInputStream(conn.getInputStream());

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

String sFileResp = sb.toString();

serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();

Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode+"::"+sFileResp);

fileInputStream.close();
dos.flush();
dos.close();
return sFileResp;

} catch (Exception e) {
e.printStackTrace();
}

服务器端接收大文件没有问题。我不想分块发送。我想在服务器端接收整个文件。

我已经绞尽脑汁好几天了,但没有运气。非常感谢任何帮助。

一揽子感谢

最佳答案

在这里您可以找到更多关于 android manages your app's memory 的信息。 .Android 限制应用程序可以使用的内存量。此限制因设备的 RAM 大小而异。发送大文件的唯一方法是分批发送。在这里你可以找到一个例子:Upload large file in Android without outofmemory error

希望对您有所帮助。

关于android - 将android中的大文件发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com