2013-01-03 17 views
5

मैंने Google ड्राइव के कुछ फ़ंक्शंस का प्रयास किया है, लेकिन मैं ड्राइव से फ़ाइल डाउनलोड नहीं कर सका।ड्राइव एपीआई जावा का उपयोग कर Google ड्राइव से फ़ाइल कैसे डाउनलोड करें?

यहाँ कोड है जो मैं

private static InputStream downloadFile(Drive service, File file) { 
    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { 
     try { 
     HttpResponse resp = 
      service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())) 
       .execute(); 
     return resp.getContent(); 
     } catch (IOException e) { 
     // An error occurred. 
     e.printStackTrace(); 
     return null; 
     } 
    } else { 
     // The file doesn't have any content stored on Drive. 
     return null; 
    } 
    } 

इस्तेमाल किया जब मैं स्क्रिप्ट चलाने पर यह पता चलता है file.getDownloadUrl() शून्य है।

मुझे यहां क्या याद आया?

अब यह निम्न पंक्ति जोड़ने के बाद क्रियान्वित है इससे पहले कि मैं डाउनलोड समारोह

File file1 = service.files().get(fileid).execute(); 
downloadFile(service,file1); 

फोन अब समस्या यह है कि 'प्रतिक्रिया' जो मैं स्क्रिप्ट से मिला की मदद से फ़ाइल डाउनलोड करने के लिए है ... ।

गूगल डॉक्स देशी स्वरूपों में
+0

मैं इस के लिए नया हूँ ... क्या सेवा यहाँ है ?? और हम इस विधि को कैसे कॉल कर सकते हैं ?? मैं समझता हूं कि फ़ाइल क्या है लेकिन सेवा – zytham

+0

"सेवा" को फ़ंक्शन को कॉल करने से पहले परिभाषित नहीं किया गया है .... –

उत्तर

4

दस्तावेज एक downloadUrl क्षेत्र के लिए नहीं होगा, बजाय आप उन्हें exportLinks संग्रह का उपयोग कर निर्यात कर सकते हैं:

https://developers.google.com/drive/manage-downloads#downloading_google_documents

+2

कूल। धन्यवाद। मुझे लगता है कि Google को 404 त्रुटि का जवाब देना चाहिए जबकि उपयोगकर्ता 'downloadUrl' या 'webContentLink' प्राप्त करने का प्रयास कर रहा है। या Google ड्राइव एसडीके को ऐसा करने पर अपवाद फेंकना चाहिए। यह चीजों को बहुत स्पष्ट कर देगा! –

0

यह है कि मैं क्या gooogle ड्राइव से एक फ़ाइल डाउनलोड करने के लिए बनाया है आशा है कि यह आप में मदद मिलेगी।

Please refer to this doc

/** Application name. */ 
private static final String APPLICATION_NAME = "Drive API Java Quickstart"; 

/** Directory to store user credentials for this application. */ 
private static final java.io.File DATA_STORE_DIR = new java.io.File(
     System.getProperty("user.home"), 
     ".credentials/n/drive-java-quickstart"); 

/** Global instance of the {@link FileDataStoreFactory}. */ 
private static FileDataStoreFactory DATA_STORE_FACTORY; 

/** Global instance of the JSON factory. */ 
private static final JsonFactory JSON_FACTORY = JacksonFactory 
     .getDefaultInstance(); 

/** Global instance of the HTTP transport. */ 
private static HttpTransport HTTP_TRANSPORT; 

/** 
* Global instance of the scopes required by this quickstart. 
* 
* If modifying these scopes, delete your previously saved credentials at 
* ~/.credentials/drive-java-quickstart 
*/ 
private static final java.util.Collection<String> SCOPES = DriveScopes 
     .all(); 

static { 
    try { 
     HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
     DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
     System.exit(1); 
    } 
} 

/** 
* Creates an authorized Credential object. 
* 
* @return an authorized Credential object. 
* @throws IOException 
*/ 
public static Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = Quickstart.class 
      .getResourceAsStream("/client_secret.json"); 
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
      JSON_FACTORY, new InputStreamReader(in)); 

    // Build flow and trigger user authorization request. 
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
      .setDataStoreFactory(DATA_STORE_FACTORY) 
      .setAccessType("offline").build(); 
    Credential credential = new AuthorizationCodeInstalledApp(flow, 
      new LocalServerReceiver()).authorize("user"); 
    System.out.println("Credentials saved to " 
      + DATA_STORE_DIR.getAbsolutePath()); 
    return credential; 
} 

/** 
* Build and return an authorized Drive client service. 
* 
* @return an authorized Drive client service 
* @throws IOException 
*/ 
public static Drive getDriveService() throws IOException { 
    Credential credential = authorize(); 
    return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) 
      .setApplicationName(APPLICATION_NAME).build(); 
} 

public static void main(String[] args) throws IOException { 
    // Build a new authorized API client service. 
    Drive service = getDriveService(); 
    // Print the names and IDs for up to 10 files. 
    FileList result = service.files().list().execute(); 
    List<File> files = result.getFiles(); 
    if (files == null || files.size() == 0) { 
     System.out.println("No files found."); 
    } else { 

     for (File file : files) { 

      String fname = file.getName(); 
      String ex = fname.substring(fname.lastIndexOf(".") + 1); 

      try { 
       Files f = service.files(); 
       HttpResponse httpResponse = null; 
       if (ex.equalsIgnoreCase("xlsx")) { 
        httpResponse = f 
          .export(file.getId(), 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
          .executeMedia(); 

       } else if (ex.equalsIgnoreCase("docx")) { 
        httpResponse = f 
          .export(file.getId(), 
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document") 
          .executeMedia(); 
       } else if (ex.equalsIgnoreCase("pptx")) { 
        httpResponse = f 
          .export(file.getId(), 
            "application/vnd.openxmlformats-officedocument.presentationml.presentation") 
          .executeMedia(); 

       } else if (ex.equalsIgnoreCase("pdf") 
         || ex.equalsIgnoreCase("jpg") 
         || ex.equalsIgnoreCase("png")) { 

        Get get = f.get(file.getId()); 
        httpResponse = get.executeMedia(); 

       } 
       if (null != httpResponse) { 
        InputStream instream = httpResponse.getContent(); 
        FileOutputStream output = new FileOutputStream(
          file.getName()); 
        try { 
         int l; 
         byte[] tmp = new byte[2048]; 
         while ((l = instream.read(tmp)) != -1) { 
          output.write(tmp, 0, l); 
         } 
        } finally { 
         output.close(); 
         instream.close(); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
संबंधित मुद्दे