gpt4 book ai didi

java - 如何在单击按钮时暂停 android 中的 Httpurl 连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:30 31 4
gpt4 key购买 nike

我是 android 开发的新手,我一直在 android 中通过 httpurlconnection 发送一个大文件。我用于通过 httpurlconnection 上传文件的代码是

 try {
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024)

// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

// TOKENID
String token = "anyvalye";
outputStream.writeBytes("Content-Disposition: form-data; name=\"Token\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
outputStream.writeBytes("Content-Length: " + token.length() + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(token + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

String connstr = null;
connstr = "Content-Disposition: form-data; name=\"UploadFile\";filename=\""
+ pathToOurFile + "\"" + lineEnd;

outputStream.writeBytes(connstr);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
TotalFileSize=bufferSize;

System.out.println("Initial Max Size" + bytesAvailable + "");
buffer = new byte[bufferSize];

// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
System.out.println("Server Response Code " + " " + serverResponseCode);
System.out.println("Server Response Message "+ serverResponseMessage);

if (serverResponseCode == 200) {
response = "true";
} else {
response = "false";
}

fileInputStream.close();
outputStream.flush();

connection.getInputStream();
//for android InputStream is = connection.getInputStream();
java.io.InputStream is = connection.getInputStream();

int ch;
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1 ) {
b.append((char)ch);
}

String responseString = b.toString();
System.out.println("response string is" + responseString); //Here is the actual output

outputStream.close();
outputStream = null;

} catch (Exception ex) {
// Exception handling
response = "error";
System.out.println("Send file Exception" + ex.getMessage() + "");
ex.printStackTrace();
}
return response;

我了解到在下载文件的过程中,我们可以暂停和恢复下载,并且有很多代码可用。我尝试了这些代码并得到了解决方案。

但是我可以在 Httpurlconnection 中暂停和恢复上传过程吗?如果我的问题很愚蠢,请原谅我,因为我是新近学习这些概念的。任何人都可以在这里帮助我暂停和恢复上传,因为据我所知没有可用的代码。

调试后,我在这段代码中得到了上传文件大小的响应,

bytesRead = fileInputStream.read(buffer, 0, bufferSize);
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}

希望得到一些帮助来更新自己。

最佳答案

```
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}
}
```

关于java - 如何在单击按钮时暂停 android 中的 Httpurl 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45477796/

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