Track the progress of file upload on Android device with BufferedInputStream
How to track the progress of file uploading in the when Content-Type
is not multipart and is "Content-Type", "application/octet-stream"
. There are solution to count in loop but they fail as original stream travels over network after writing on output stream.
Following is my piece of code
HttpURLConnection urlConnection = null;
URL url = new URL(strUrl);
if (Utils.isValidURLForHttp(strUrl)) {
urlConnection = (HttpURLConnection) url.openConnection();
} else {
urlConnection = (HttpsURLConnection) url.openConnection();
}
String headerString = "";
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// do stuff
urlConnection.setRequestProperty(key, value);
headerString += key + ": " + value + "n";
}
}
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setConnectTimeout(1000 * Constants.CONNECTION_TIME_OUT_SEC);
urlConnection.setReadTimeout(1000 * Constants.READ_TIME_OUT_IMAGE_UPLOAD_SEC);
String filePath = uri.getPath();
File file = new File(filePath);
OutputStream outputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 32768;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte buffer = new byte[(int) file.length()/* bufferSize*/];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
Log.d("tagProgressUpload","Upload ");
}
outputStream.flush();
fileInputStream.close();
///
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception ex) {
inputStream = new BufferedInputStream(urlConnection.getErrorStream());
}
String jsonResponse = readResponseContents(inputStream);
String responseHeaders = getResponseHeaders(urlConnection);
Logger.getInstance().logInfo(strUrl +
"n" + Constants.APIVerbs.DELETE +
"n" + headerString +
"Request body: " + body +
"n" + "Status Code: " + urlConnection.getResponseCode() +
"n" + "ResponseHeaders: " + responseHeaders +
"n" + "Response: " + jsonResponse
);
Response response = new Response();
response.setResponse(jsonResponse);
response.setStatusCode(urlConnection.getResponseCode());
response.setStatusMessage(urlConnection.getResponseMessage());
HashMap<String, String> headers = new HashMap<>();
for (Map.Entry<String, List<String>> entries : urlConnection.getHeaderFields().entrySet()) {
headers.put(entries.getKey(), entries.getValue().get(0));
}
response.setResponseHeaders(headers);
return response;
upload progress
add a comment |
How to track the progress of file uploading in the when Content-Type
is not multipart and is "Content-Type", "application/octet-stream"
. There are solution to count in loop but they fail as original stream travels over network after writing on output stream.
Following is my piece of code
HttpURLConnection urlConnection = null;
URL url = new URL(strUrl);
if (Utils.isValidURLForHttp(strUrl)) {
urlConnection = (HttpURLConnection) url.openConnection();
} else {
urlConnection = (HttpsURLConnection) url.openConnection();
}
String headerString = "";
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// do stuff
urlConnection.setRequestProperty(key, value);
headerString += key + ": " + value + "n";
}
}
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setConnectTimeout(1000 * Constants.CONNECTION_TIME_OUT_SEC);
urlConnection.setReadTimeout(1000 * Constants.READ_TIME_OUT_IMAGE_UPLOAD_SEC);
String filePath = uri.getPath();
File file = new File(filePath);
OutputStream outputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 32768;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte buffer = new byte[(int) file.length()/* bufferSize*/];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
Log.d("tagProgressUpload","Upload ");
}
outputStream.flush();
fileInputStream.close();
///
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception ex) {
inputStream = new BufferedInputStream(urlConnection.getErrorStream());
}
String jsonResponse = readResponseContents(inputStream);
String responseHeaders = getResponseHeaders(urlConnection);
Logger.getInstance().logInfo(strUrl +
"n" + Constants.APIVerbs.DELETE +
"n" + headerString +
"Request body: " + body +
"n" + "Status Code: " + urlConnection.getResponseCode() +
"n" + "ResponseHeaders: " + responseHeaders +
"n" + "Response: " + jsonResponse
);
Response response = new Response();
response.setResponse(jsonResponse);
response.setStatusCode(urlConnection.getResponseCode());
response.setStatusMessage(urlConnection.getResponseMessage());
HashMap<String, String> headers = new HashMap<>();
for (Map.Entry<String, List<String>> entries : urlConnection.getHeaderFields().entrySet()) {
headers.put(entries.getKey(), entries.getValue().get(0));
}
response.setResponseHeaders(headers);
return response;
upload progress
add a comment |
How to track the progress of file uploading in the when Content-Type
is not multipart and is "Content-Type", "application/octet-stream"
. There are solution to count in loop but they fail as original stream travels over network after writing on output stream.
Following is my piece of code
HttpURLConnection urlConnection = null;
URL url = new URL(strUrl);
if (Utils.isValidURLForHttp(strUrl)) {
urlConnection = (HttpURLConnection) url.openConnection();
} else {
urlConnection = (HttpsURLConnection) url.openConnection();
}
String headerString = "";
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// do stuff
urlConnection.setRequestProperty(key, value);
headerString += key + ": " + value + "n";
}
}
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setConnectTimeout(1000 * Constants.CONNECTION_TIME_OUT_SEC);
urlConnection.setReadTimeout(1000 * Constants.READ_TIME_OUT_IMAGE_UPLOAD_SEC);
String filePath = uri.getPath();
File file = new File(filePath);
OutputStream outputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 32768;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte buffer = new byte[(int) file.length()/* bufferSize*/];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
Log.d("tagProgressUpload","Upload ");
}
outputStream.flush();
fileInputStream.close();
///
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception ex) {
inputStream = new BufferedInputStream(urlConnection.getErrorStream());
}
String jsonResponse = readResponseContents(inputStream);
String responseHeaders = getResponseHeaders(urlConnection);
Logger.getInstance().logInfo(strUrl +
"n" + Constants.APIVerbs.DELETE +
"n" + headerString +
"Request body: " + body +
"n" + "Status Code: " + urlConnection.getResponseCode() +
"n" + "ResponseHeaders: " + responseHeaders +
"n" + "Response: " + jsonResponse
);
Response response = new Response();
response.setResponse(jsonResponse);
response.setStatusCode(urlConnection.getResponseCode());
response.setStatusMessage(urlConnection.getResponseMessage());
HashMap<String, String> headers = new HashMap<>();
for (Map.Entry<String, List<String>> entries : urlConnection.getHeaderFields().entrySet()) {
headers.put(entries.getKey(), entries.getValue().get(0));
}
response.setResponseHeaders(headers);
return response;
upload progress
How to track the progress of file uploading in the when Content-Type
is not multipart and is "Content-Type", "application/octet-stream"
. There are solution to count in loop but they fail as original stream travels over network after writing on output stream.
Following is my piece of code
HttpURLConnection urlConnection = null;
URL url = new URL(strUrl);
if (Utils.isValidURLForHttp(strUrl)) {
urlConnection = (HttpURLConnection) url.openConnection();
} else {
urlConnection = (HttpsURLConnection) url.openConnection();
}
String headerString = "";
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// do stuff
urlConnection.setRequestProperty(key, value);
headerString += key + ": " + value + "n";
}
}
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setConnectTimeout(1000 * Constants.CONNECTION_TIME_OUT_SEC);
urlConnection.setReadTimeout(1000 * Constants.READ_TIME_OUT_IMAGE_UPLOAD_SEC);
String filePath = uri.getPath();
File file = new File(filePath);
OutputStream outputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 32768;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte buffer = new byte[(int) file.length()/* bufferSize*/];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
Log.d("tagProgressUpload","Upload ");
}
outputStream.flush();
fileInputStream.close();
///
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
} catch (Exception ex) {
inputStream = new BufferedInputStream(urlConnection.getErrorStream());
}
String jsonResponse = readResponseContents(inputStream);
String responseHeaders = getResponseHeaders(urlConnection);
Logger.getInstance().logInfo(strUrl +
"n" + Constants.APIVerbs.DELETE +
"n" + headerString +
"Request body: " + body +
"n" + "Status Code: " + urlConnection.getResponseCode() +
"n" + "ResponseHeaders: " + responseHeaders +
"n" + "Response: " + jsonResponse
);
Response response = new Response();
response.setResponse(jsonResponse);
response.setStatusCode(urlConnection.getResponseCode());
response.setStatusMessage(urlConnection.getResponseMessage());
HashMap<String, String> headers = new HashMap<>();
for (Map.Entry<String, List<String>> entries : urlConnection.getHeaderFields().entrySet()) {
headers.put(entries.getKey(), entries.getValue().get(0));
}
response.setResponseHeaders(headers);
return response;
upload progress
upload progress
edited Nov 15 '18 at 9:44
Vladimir Vagaytsev
2,19592327
2,19592327
asked Nov 15 '18 at 8:28
saqib mirzasaqib mirza
144
144
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315168%2ftrack-the-progress-of-file-upload-on-android-device-with-bufferedinputstream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315168%2ftrack-the-progress-of-file-upload-on-android-device-with-bufferedinputstream%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown