2011-02-17 16 views
8

आशा है कि मैं इसे सही ढंग से समझाऊंगा ... मैं अपने वेब सर्वर से कनेक्ट करने और एक फ़ाइल डाउनलोड करने में सक्षम हूं। अब मैं जो करने का प्रयास कर रहा हूं वह कनेक्ट है मेरा सर्वर और किसी विशेष फ़ोल्डर से सभी मक्खियों को डाउनलोड करें। इस मामले में मैं छवियों को डाउनलोड करना चाहता हूं। इस कोड को मैं एक एकल फाइल डाउनलोड करने के लिए इस्तेमाल करते हैं ...एंड्रॉइड - सर्वर पर किसी फ़ोल्डर से सभी फाइलें डाउनलोड करें

URL url = new URL("http://127.0.0.1/Folder/file.csv"); 
      URLConnection conexion = url.openConnection(); 
      conexion.connect(); 
      int lenghtOfFile = conexion.getContentLength(); 
      InputStream is = url.openStream(); 
      File testDirectory = 
      new File(Environment.getExternalStorageDirectory()+"/Folder"); 
      if(!testDirectory.exists()){ 
       testDirectory.mkdir(); 
      } 
      FileOutputStream fos = new FileOutputStream(testDirectory+"/file.csv"); 
      byte data[] = new byte[1024]; 
      int count = 0; 
      long total = 0; 
      int progress = 0; 
      while ((count=is.read(data)) != -1){ 
       total += count; 
       int progress_temp = (int)total*100/lenghtOfFile; 
       if(progress_temp%10 == 0 && progress != progress_temp){ 
        progress = progress_temp; 
       } 
       fos.write(data, 0, count); 
      } 
      is.close(); 
      fos.close(); 

मैं इस कोड कैसे जोड़ सकते हैं यह है कि फ़ोल्डर से सभी फ़ाइलों को डाउनलोड करने के लिए?

+0

अरे मुझे अपने एंड्रॉइड पर एएस 3 का उपयोग करके फ़ाइलों को डाउनलोड करने में मदद की ज़रूरत है। आपने कहा कि यह स्क्रिप्ट काम करता है? क्या आप इसे काम करने में मदद कर सकते हैं क्योंकि यह मेरे लिए काम नहीं कर रहा है? –

+0

मुझे बस एक फ़ाइल फ़ाइल की आवश्यकता है –

उत्तर

7

मैं आपको सर्वर के अंत में एक स्क्रिप्ट प्राप्त करने का सुझाव दूंगा, जो आपको पहले फ़ोल्डर में सभी फाइलों की सूची देता है, और फिर आपका एप्लिकेशन प्रत्येक फ़ाइल को एक-एक करके डाउनलोड करता है।

+0

हम्म यह एकमात्र तरीका है? यदि हां, तो क्या आपको पता है कि मैं ऐसी स्क्रिप्ट खोजने के लिए कहां जा सकता हूं? फ़ोल्डर में अद्वितीय छवि नामों के साथ विभिन्न प्रकार की छवियां होने जा रही हैं, क्या स्क्रिप्ट छवियों के नाम रखने में सक्षम होगी – Beginner

+0

क्या सिर्फ इतना कहने का कोई तरीका नहीं है कि पूरे फ़ोल्डर को डाउनलोड करें और इसे एसडी कार्ड – Beginner

+0

I पर कॉपी करें। ऐसा मत सोचो कि आपने जो कुछ कहा है वह करने का एक तरीका है। हालांकि, स्क्रिप्ट आपको नवीनतम सूची देने के लिए पर्याप्त गतिशील होगी –

3
public class DownloadFileService extends Service implements ConfigCommonVars { 

private static final int NOTIFICATION = 0; 
private NotificationManager mNM; 
File SDCardRoot = Environment.getExternalStorageDirectory(); 

ArrayList<String> mServerFileListApp = new ArrayList<String>(); 
ArrayList<String> mDeviceFileListApp = new ArrayList<String>(); 

@Override 
public void onCreate() { 
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    // Display a notification about us starting. We put an icon in the status bar. 
    showNotification(); 
    Toast.makeText(this, " device Services ",Toast.LENGTH_LONG).show(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.i("LocalService", "Received start id " + startId + ": " + intent); 
    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 

    File directory = new File(SDCardRoot+DOC_FOLDER_NAME); 

    // create directory if not exists 
    if(!directory.exists()) 
    { 
     if(directory.mkdirs()) //directory is created; 
      Log.i(" download ","App dir created"); 
     else 
      Log.w(" download ","Unable to create app dir!"); 
    } 

    mDeviceFileListApp = getDeviceFiles(); 
    Toast.makeText(this, " device file "+mDeviceFileListApp.toString(),Toast.LENGTH_LONG).show(); 

     new Thread(new Runnable() { 
      public void run() { 
       try { 
        mServerFileListApp = getServerFiles(); 
       }catch(Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 

    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    // Cancel the persistent notification. 
    mNM.cancel(NOTIFICATION); 

    // Tell the user we stopped. 
    Toast.makeText(this, R.string.local_service_stopped, Toast.LENGTH_SHORT).show(); 
} 

private void showNotification() { 

    /*// In this sample, we'll use the same text for the ticker and the expanded notification 
    CharSequence text = getText(R.string.local_service_started); 

    // Set the icon, scrolling text and timestamp 
    Notification notification = new Notification(R.drawable.stat_sample, text,System.currentTimeMillis()); 

    // The PendingIntent to launch our activity if the user selects this notification 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, this.Controller.class), 0); 

    // Set the info for the views that show in the notification panel. 
    notification.setLatestEventInfo(this, getText(R.string.local_service_label),text, contentIntent); 

    // Send the notification. 
    mNM.notify(NOTIFICATION, notification); 
    */ 

} 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public ArrayList<String> getDeviceFiles() 
{ 
    mDeviceFileListApp = new ArrayList<String>(); 

    File directory = new File(SDCardRoot+DOC_FOLDER_NAME); 
    if(directory.length()!=0) // check no of files 
    { 
     for (File file : directory.listFiles()) { 
      if (file.isFile()) 
       mDeviceFileListApp.add(file.getName());   
     } 
    } 
    return mDeviceFileListApp; 
} 

public ArrayList<String> getServerFiles() 
{ 
    InputStream inputStream = null; 
    JSONArray mFileArray = null; 
    String mfileNames ; 
    mServerFileListApp = new ArrayList<String>(); 


    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(HTTP+SITE_URL+WEB_SERVICES_PATH+"bmservicecontroller.php?PAGE=loginpage&OPTION=downloadFile&TYPE=1"); 

    // get list of file in download folder 
    try 
    { 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity httpEntity = response.getEntity(); 
     inputStream = httpEntity.getContent(); 

     BufferedReader reader= new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 
     StringBuilder builder= new StringBuilder(); 
     char[] buf = new char[1000]; 
     int l = 0; 
     while (l >= 0) 
     { 
      builder.append(buf, 0, l); 
      l = reader.read(buf); 
     } 
     inputStream.close(); 

     JSONTokener tokener = new JSONTokener(builder.toString()); 
     JSONObject finalResult = new JSONObject(tokener); 

     mFileArray = finalResult.getJSONObject("FSSEAPI").getJSONArray("FileName"); 

     for (int i = 0; i < mFileArray.length(); i++) 
     { 
      try { 
       mfileNames = mFileArray.getString(i); 
       mServerFileListApp.add(mfileNames); 

      } catch (Exception e) { 
       //showError("File Not Found " + mfileNames); 
       e.printStackTrace(); 
      } 
     } // for ends 

     String temp; 
     for (int i=0; i<mServerFileListApp.size(); i++) 
     { 
      temp = mServerFileListApp.get(i); 
      if(! mDeviceFileListApp.contains(temp)) 
      { 
       Log.i(" File Download Start ",HTTP+SITE_URL+DOWNLOAD_PATH+temp); 
       downloadFileManager(HTTP+SITE_URL+DOWNLOAD_PATH,temp);     
      } 
      else 
      { 
       // check and Delete File Exists 
       Log.i(" File Deleted ",HTTP+SITE_URL+DOWNLOAD_PATH+temp); 
       /* 
       File checkFile = new File(SDCardRoot+DOC_FOLDER_NAME+temp); 

       if(checkFile.exists()) 
        if(checkFile.delete()) 
         Log.i(" File Deleted ",HTTP+SITE_URL+DOWNLOAD_PATH+temp); 
        else 
         Log.i(" File Not Delete ",HTTP+SITE_URL+DOWNLOAD_PATH+temp); 
       */ 
      } 
     } 

    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
     //showError("File Download Error "); 
    } 
    catch (JSONException e) 
    { 
     e.printStackTrace(); 
     //showError("File Download Error "); 
    } 

    return mServerFileListApp; 
} 

public void downloadFileManager(String path,String file) 
{ 
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path+file)); 
    request.setDescription(file+"descrition"); 
    request.setTitle(file+"title"); 
    // in order for this if to run, you must use the android 3.2 to compile your app 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     //request.allowScanningByMediaScanner(); 
     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
    } 
    request.setDestinationInExternalPublicDir(DOC_FOLDER_NAME, file); 

    // get download service and enqueue file 
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
    manager.enqueue(request); 
} 
} 
संबंधित मुद्दे