انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
!!!!!!!!!!! دریافت فایل و ذخیره در حافظه از یکurl !!!!!!!!!!!!!

!!!!!!!!!!! دریافت فایل و ذخیره در حافظه از یکurl !!!!!!!!!!!!!

#1
میخوام ی دانلودر که فایلی رو از ادرسی دریافت و در حافظه با نام و فرمت فایل خودش ذخیره کنم اما نمیدونم چکار باید بکنم!!!
من از این کد برای دریافت و ذخیره استفاده میکنم اما مشکل اینجاست ک با اسم و فرمتی ک بطور پیش فرض نوشته شده ذخیرهمیشه، من میخام همانطور ک ادرس رو از متغییر میگیره،نام و فرمت رو هم بگیره

کد پی‌اچ‌پی:
private class DownloadTask extends AsyncTask<StringIntegerString> {
 
 
   private Context context;
 
 
   public DownloadTask(Context context) {
 
       this.context context;
 
   }
 
 
   @Override
    protected String doInBackground
(String... sUrl) {
 
       // take CPU lock to prevent CPU from going off if the user 
 
       // presses the power button during download
 
       PowerManager pm = (PowerManagercontext.getSystemService(Context.POWER_SERVICE);
 
       PowerManager.WakeLock wl pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
 
            getClass().getName());
 
       wl.acquire();
 
 
       try {
 
           InputStream input null;
 
           OutputStream output null;
 
           HttpURLConnection connection null;
 
           try {
 
               URL url = new URL(sUrl[0]);
 
               connection = (HttpURLConnectionurl.openConnection();
 
               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();
 
 
               // download the file
 
               input connection.getInputStream();
 
               output = new FileOutputStream("/sdcard/myFiledown.format");  //   /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())
 
                       return null;
 
                   total += count;
 
                   // publishing the progress....
 
                   if (fileLength 0// only if total length is known
 
                       publishProgress((int) (total 100 fileLength));
 
                   output.write(data0count);
 
               }
 
           } catch (Exception e) {
 
               return e.toString();
 
           } finally {
 
               try {
 
                   if (output != null)
 
                       output.close();
 
                   if (input != null)
 
                       input.close();
 
               
 
               catch (IOException ignored) { }
 
 
               if (connection != null)
 
                   connection.disconnect();
 
           }
 
       } finally {
 
           wl.release();
 
       }
 
       return null;
 
   }
 
    
    
@Override
    protected void onPreExecute
() {
 
       super.onPreExecute();
 
       mProgressDialog.show();
 
   }
 
 
   @Override
    protected void onProgressUpdate
(Integer... progress) {
 
       super.onProgressUpdate(progress);
 
       // if we get here, length is known, now set indeterminate to false
 
       mProgressDialog.setIndeterminate(false);
 
       mProgressDialog.setMax(100);
 
       mProgressDialog.setProgress(progress[0]);
 
   }
 
 
   @Override
    protected void onPostExecute
(String result) {
 
       mProgressDialog.dismiss();
 
       if (result != null)
 
           Toast.makeText(context,"Download error: "+resultToast.LENGTH_LONG).show();
 
       else
            Toast
.makeText(context,"File downloaded"Toast.LENGTH_SHORT).show();
 
   }
 
    

با این کد فایل دانلود میشه
کد پی‌اچ‌پی:
final DownloadTask downloadTask = new DownloadTask(MainActivity.this); 
 
               downloadTask.execute("http://www.example.com/file.format"); 

حالا من میخام همانطور ک با کد بالا آدرس رو ب کلاس دانلود فرستادم،نام و فرمت رو هم بفرستم ن اینکه خودش مثل کد زیر استاتیک پر کنه
کد پی‌اچ‌پی:
output = new FileOutputStream("/sdcard/myFiledown.format"); 

لطفا کمک کنید
مرســـــــــــی
پاسخ

!!!!!!!!!!! دریافت فایل و ذخیره در حافظه از یکurl !!!!!!!!!!!!!

#2
دوستان کسی نمیتونه کمک کنه ؟؟
پاسخ
 سپاس شده توسط شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان