Any one help me..
My problem is that when i am downloading file sometimes server is stooped & download is done only 50% after server start I want download from 51%. because 50% download is completed before server stop.
My code is below..but its not working.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
Thanks.
Related
I want to make a download manager for android I want to know how can I add
pause and resume functionality I am using HttpUrlConnection class of android
for getting the data.I also want to know how can I access the InputStream which I get from HttpUrlConnection.getInputStream() which get Interrupted because of the Internet connection(say user turn off internet connection).
any sample code will be helpful
Downloader example here
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
Credits: https://stackoverflow.com/users/705297/pomatu
I am using the following code for downloading file from server:
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.connect();
StorageTools.maintainMemoryEmptyThreshold(25);
if (tempFolderFile.createNewFile()) {
tempFolderFile.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(tempFolderFile);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[8096];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
LOGGER.debug("Progress: downloadedSize: " + downloadedSize + "totalSize: " + totalSize);
}
fileOutput.close();
if (downloadedSize == totalSize) {
LOGGER.info("Downloaded at " + tempFolderFile.getAbsolutePath());
StorageTools.moveFile(tempFolder, targetFolder, fileName);
return true;
}
I trigger the download using a new thread into a tempfolder, move it into required folder upon download completion. How to maintain a cap on the number of active download threads and add remaining downloads to a queue ?
Also, following are my concerns in this regard:
Is there any way in which I can incorporate a threadpool executor for queuing downloads using above function ?
Does the buffersize impact the download speed ?
Please check https://developer.android.com/reference/android/app/DownloadManager.html
buffersize will not impact much. I think it should be in safe limit.
I want to be able to open a http connection to a given file in Android and start downloading it.
I also have to be able to pause the download at some point and resume it later.
How is this achieved in Android? I don't want to start the download all over again.
Such a downloader has been posted here:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
I have an app to download zip file content from drop box (which is publicly shared path). i wrote download code using HttpURLConnection but its not working as intended and instead is downloading a small portion (after download zip file showing 31 kb but its original size is 3mb). i am attching my code. please help me to solve this.
URL url = new URL("drop box public share url");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setAllowUserInteraction(false);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setConnectTimeout(5 * 1000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"/download/sample.zip");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
onProgressUpdate(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
inputStream.close();
It seems that the method call:
setDoOuput(true);
makes the request a POST (see
What exactly does URLConnection.setDoOutput() affect?)
Removing it seems to fix the issue.
Try to use plain URLConnection not HttpURLConnection. And see the output.
i am using below code to downloading file from URL.
URL url = new URL(mCoreContent.VideoURI);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
lenghtOfFile = c.getContentLength();
InputStream input = c.getInputStream();
BufferedInputStream bis = new BufferedInputStream(input);
ByteArrayOutputStream output = new ByteArrayOutputStream((int) lenghtOfFile);
int buffer = 4096;
byte data[] = new byte[buffer];
int current = 0;
while ((current = bis.read(data, 0, buffer)) != -1) {
output.write(data, 0, current);
}
i tested above code with two different device. one device taking less than a minute downloading the file and another takes more than 5-10 to downloading same with same network connecting. i tested multiple times and i am getting the same result. Please help.
Thanks in advance.