2014-10-01 2 views
0

.Net парень, работающий на Java приложенииGoogle Drive загрузка файла, и чтение файла

Я загружаю, используя следующий пример моей отправной точки (я имею эту работу). Это показывает использование FileContent, требующего java.io.File, который не содержит фактического файла только указателя на фактический файл.

Мы загружаем с веб-сайта и пытаемся вставить его в дисковод, я бы предпочел сделать это, используя поток памяти, например, пример .Net. Я не вижу этого, глядя на класс FileContent. Итак, мои поиски: Есть ли способ вставить файл в Google диск, который находится в памяти, а не на жестком диске?

private static File insertFile(Drive service, String title, String description, 
    String parentId, String mimeType, String filename) { 
// File's metadata. 
File body = new File(); 
body.setTitle(title); 
body.setDescription(description); 
body.setMimeType(mimeType); 

// Set the parent folder. 
if (parentId != null && parentId.length() > 0) { 
    body.setParents(
     Arrays.asList(new ParentReference().setId(parentId))); 
} 

// File's content. 
java.io.File fileContent = new java.io.File(filename); 
FileContent mediaContent = new FileContent(mimeType, fileContent); 
try { 
    File file = service.files().insert(body, mediaContent).execute(); 

    // Uncomment the following line to print the File ID. 
    // System.out.println("File ID: " + file.getId()); 

    return file; 
} catch (IOException e) { 
    System.out.println("An error occured: " + e); 
    return null; 
} 
} 

ответ

0

Override AbstractInputStream построить свой собственный FileContent

package com; 

import java.io.IOException; 
import java.io.InputStream; 
import com.google.api.client.http.AbstractInputStreamContent; 
import com.google.api.client.util.Preconditions; 

public class FileContent extends AbstractInputStreamContent { 

private InputStream inputStream = null; 
private long inputLength = 0; 

public FileContent(String type, InputStream pInputStream) throws IOException { 
    super(type); 
    this.inputStream = Preconditions.checkNotNull(pInputStream);   
    this.inputLength = this.inputStream.available(); 
} 

public long getLength() throws IOException { 
    return this.inputLength; 
} 

public boolean retrySupported() { 
    return false; 
} 

@Override 
public InputStream getInputStream() throws IOException { 
    return this.inputStream; 
} 
} 
Смежные вопросы