Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在上传到Google文档时覆盖具有相同文件名的zip文件_Java - Fatal编程技术网

Java 如何在上传到Google文档时覆盖具有相同文件名的zip文件

Java 如何在上传到Google文档时覆盖具有相同文件名的zip文件,java,Java,我有下面的代码片段上传zip文件而不转换为谷歌文档 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.cli

我有下面的代码片段上传zip文件而不转换为谷歌文档

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!");            
    }
}
   // 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();
要编译上述代码,需要以下4个库

但是,我意识到,当我一次又一次地尝试上载具有相同文件名的文件时,以前上载的文件将不会被覆盖

这是我执行上述代码4次的结果


我可以知道,在上传到Google Doc的过程中,如何覆盖具有相同文件名的文件吗?

以下是如何在Google Doc中覆盖文件

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!");            
    }
}
   // 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();
要获得完整的代码段,请参阅实用程序函数