PDA

View Full Version : اضافه کردن قابلیت Resume به دانلود فایل + کد



hamedg1366
چهارشنبه 30 دی 1394, 21:36 عصر
با سلام و ادب خدمت دوستان قدیمی و جدید


کدهای بنده برای دانلود فایل :


try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
//connection.connect();
Log.i("Pdf_download", "1");

///////////////////////////////////////////////////////////

File file=new File("/sdcard/file_name.extension");
if(file.exists()){
fileLength = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}else {
fileLength = connection.getContentLength();
}
connection.connect();
///////////////////////////////////////////////

// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}

// this will be useful to display download percentage
// might be -1: server did not report the length
//int fileLength = connection.getContentLength();

Log.i("Pdf_download", "2");
// download the file
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/file_name.extension");

byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled()) {
input.close();
return null;
}
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
Log.i("Pdf_download", "3");
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}




لاگ خطا :


Server returned HTTP 206 Partial Content



با تشکر از همه عزیزان

hamedg1366
چهارشنبه 30 دی 1394, 21:41 عصر
ناگفته نمونه ان تیکه کد توی سایت استک آور فلو بیشترین لایک رو خورده فقط من نمیتونم بفهمم این خط چیه و اکلیپس بهش خطا میده

if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.E CM_DOWNLOADING)



try {
downloaded=0;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.E CM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
}
}
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
connection.connect();
size=connection.getContentLength();
Dialog.setMax(size);
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;
System.out.println(downloaded);
onProgressUpdate((int)(downloaded*100/size));
}

succes=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
bout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

msroid
چهارشنبه 30 دی 1394, 22:33 عصر
سلام

اون خط کد یعنی اینکه شما وضعیت دانلود رو جایی ذخیره کردی و بعد هنگام دانلود دوباره اونو چک میکنی ببینی در چه وضعیتی بوده. این یه تیکه کد برای نشون دادن این قضیه هست وگرنه جزیی از کلمات و یا دستورات کلیدی نیست و خودت باید یه همچین مکانیزمی رو در نظر بگیری...!!

hamedg1366
چهارشنبه 30 دی 1394, 22:41 عصر
سلام

اون خط کد یعنی اینکه شما وضعیت دانلود رو جایی ذخیره کردی و بعد هنگام دانلود دوباره اونو چک میکنی ببینی در چه وضعیتی بوده. این یه تیکه کد برای نشون دادن این قضیه هست وگرنه جزیی از کلمات و یا دستورات کلیدی نیست و خودت باید یه همچین مکانیزمی رو در نظر بگیری...!!


مرسی دوست عزیز ، برای این موضوع پیشنهادی دارین ؟
در هنگام ایست و اجرای دوباره اسینک تسک چطور میشه ازش استفاده کرد/؟

hamedg1366
چهارشنبه 30 دی 1394, 22:48 عصر
سلام

اون خط کد یعنی اینکه شما وضعیت دانلود رو جایی ذخیره کردی و بعد هنگام دانلود دوباره اونو چک میکنی ببینی در چه وضعیتی بوده. این یه تیکه کد برای نشون دادن این قضیه هست وگرنه جزیی از کلمات و یا دستورات کلیدی نیست و خودت باید یه همچین مکانیزمی رو در نظر بگیری...!!

یادم رفت بگم ؛ برای همین موضوع نباید همین کافی باشه :

File file=new File("/sdcard/file_name.extension");
if(file.exists()){
fileLength = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}else {
fileLength = connection.getContentLength();
}
connection.connect();

hamedg1366
پنج شنبه 01 بهمن 1394, 22:13 عصر
Any ideas?

Help ME