如何在上传到Google Doc期间用相同的文件名覆盖zip文件

时间:2012-01-21 08:00:17

标签: java

我有以下代码段上传zip文件而不转换为Google文档。

package sample.docs;

import com.google.gdata.client.docs.*;
import com.google.gdata.data.docs.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.MalformedURLException;
import com.google.gdata.client.media.ResumableGDataFileUploader;
import com.google.gdata.client.uploader.FileUploadData;
import com.google.gdata.client.uploader.ProgressListener;
import com.google.gdata.client.uploader.ResumableHttpFileUploader;
import com.google.gdata.data.media.MediaFileSource;
import java.io.File;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DocumentListDemo {

    private static class FileUploadProgressListener implements ProgressListener {

        final CountDownLatch countDownLatch = new CountDownLatch(1);

        @Override
        public synchronized void progressChanged(ResumableHttpFileUploader uploader)
        {
            final String fileId = ((FileUploadData) uploader.getData()).getFileName();
            switch(uploader.getUploadState()) {
            case COMPLETE:
            case CLIENT_ERROR:
                countDownLatch.countDown();
                System.out.println(fileId + ": Completed");
                break;

            case IN_PROGRESS:
                System.out.println(fileId + ":" + String.format("%3.0f", uploader.getProgress() * 100) + "%");
                break;

            case NOT_STARTED:
                System.out.println(fileId + ":" + "Not Started");
                break;
            }
        }

        public void await() throws InterruptedException {
            countDownLatch.await();
        }
    }

    public static void main(String[] args) throws MalformedURLException, IOException, ServiceException, InterruptedException, DocumentListException {
        int MAX_CONCURRENT_UPLOADS = 10;
        int PROGRESS_UPDATE_INTERVAL = 1000;
        int DEFAULT_CHUNK_SIZE = 10485760;

        DocsService client = new DocsService("JStock");
        client.setUserCredentials("yancheng.cheok@gmail.com", "this-is-my-password");

        // Create a listener
        FileUploadProgressListener listener = new FileUploadProgressListener();

        // Pool for handling concurrent upload tasks
        ExecutorService executor = Executors.newFixedThreadPool(MAX_CONCURRENT_UPLOADS);

        File file = new File("c:\\Pictures.zip");
        String contentType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
        MediaFileSource mediaFile = new MediaFileSource(file, contentType);
        URL createUploadUrl = new URL("https://docs.google.com/feeds/upload/create-session/default/private/full");
        ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(client, createUploadUrl, mediaFile, null)
            .title(mediaFile.getName())
            .chunkSize(DEFAULT_CHUNK_SIZE).executor(executor)
            .trackProgress(listener, PROGRESS_UPDATE_INTERVAL)
            .build();
        uploader.start();


        // Wait for completion.
        listener.await();

        // Thread clean up.
        executor.shutdownNow();
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

        System.out.println("done!");            
    }
}

要编译上述代码,需要以下4个库。

enter image description here

但是,我发现,当我尝试一次又一次地使用相同文件名上传文件时,之前上传的文件将不会被覆盖

如果我执行上述代码4次,这就是结果。

enter image description here

我可以知道,在上传到Google文档期间,如何使用相同的文件名覆盖文件?

1 个答案:

答案 0 :(得分:0)

以下是覆盖文件在Google文档中的工作方式。

   // Rename and overwrite.
   documentListEntry.setTitle(new PlainTextConstruct(getGoogleDocTitle(checksum, date, version)));
   uploader = new ResumableGDataFileUploader.Builder(client, createUploadUrl, mediaFile, documentListEntry)
   .title(getGoogleDocTitle(checksum, date, version))
   .chunkSize(DEFAULT_CHUNK_SIZE).executor(executor)
   .trackProgress(listener, PROGRESS_UPDATE_INTERVAL).requestType(ResumableGDataFileUploader.RequestType.UPDATE)
   .build();

要获取完整的代码段,请参阅saveToGoogleDoc实用程序功能。