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
با تشکر از همه عزیزان
کدهای بنده برای دانلود فایل :
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
با تشکر از همه عزیزان