Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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 在ListenableFuture中执行post任务不工作_Java_Multithreading_Asynchronous_Guava - Fatal编程技术网

Java 在ListenableFuture中执行post任务不工作

Java 在ListenableFuture中执行post任务不工作,java,multithreading,asynchronous,guava,Java,Multithreading,Asynchronous,Guava,当我尝试轻松上传文件时,请使用ListenableFuture,但它不起作用。 如果它是syn,它工作正常 ListenableFuture不支持执行远程发布?我发现了一个示例,它使用URL.openStream()workOK for get private void asynUploadFileToRemote(final String uptoken, final String key, final File file, final PutExtra extra) { fina

当我尝试轻松上传文件时,请使用ListenableFuture,但它不起作用。 如果它是syn,它工作正常

ListenableFuture不支持执行远程发布?我发现了一个示例,它使用URL.openStream()workOK for get

  private void asynUploadFileToRemote(final String uptoken, final String key, final File file, final PutExtra extra) {
    final ListenableFuture<String> future = pool.submit(new Callable<String>() {
        @Override
        public String call() throws Exception {
            logger.info("starting to upload file");
            // here is to upload a file to remote.
            PutRet putRet = IoApi.put(uptoken, key, file, extra);
            logger.info("end to upload file"); //this log never excute.
            return putRet.getKey();
        }
    });

    future.addListener(new Runnable() {
        @Override
        public void run() {
            try {
                //recerve nothing here
                String key = future.get();
                logger.info("uploadkey:" + key);
            } catch (InterruptedException e) {
                logger.error("Interrupted", e);
            } catch (ExecutionException e) {
                logger.error("Exception in task", e.getCause());
            }
        }
    }, MoreExecutors.sameThreadExecutor());
}

你的任务到底在执行吗?(是否“开始上传文件”已被记录?)您可以添加一个到您正在使用的http库的链接吗?@luke,它是一个存储API。
public static PutRet put(String uptoken, String key, File file,
                         PutExtra extra) {

    if (!file.exists() || !file.canRead()) {
        return new PutRet(new CallRet(400, new Exception(
                "File does not exist or not readable.")));
    }
    if (key == null) {
        key = UNDEFINED_KEY;
    }

    MultipartEntity requestEntity = new MultipartEntity();
    try {
        requestEntity.addPart("token", new StringBody(uptoken));
        FileBody fileBody = new FileBody(file);
        requestEntity.addPart("file", fileBody);
        requestEntity.addPart("key", new StringBody(key));

        if (extra.checkCrc != NO_CRC32) {
            if (extra.crc32 == 0) {
                return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
            }
            requestEntity.addPart("crc32", new StringBody(extra.crc32 + ""));
        }
    } catch (Exception e) {
        e.printStackTrace();
        return new PutRet(new CallRet(400, e));
    }

    String url = Config.UP_HOST;
    CallRet ret = new Client().callWithMultiPart(url, requestEntity);
    return new PutRet(ret);
}