Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我在使用Java操作 aliyunpan cli 但是上传失败,但是使用命令行直接操作是正常的 #393

Closed
uhaorph1636 opened this issue Feb 28, 2024 · 1 comment

Comments

@uhaorph1636
Copy link

这是使用命令行的操作结果
image

`
public static String upload(File from, File to, long timeout) throws Exception {
List command = new ArrayList<>();
command.add("/opt/homebrew/bin/aliyunpan");
command.add("upload");
command.add("--ow");
command.add(from.getAbsolutePath());
command.add(to.getAbsolutePath());

    return exec(timeout, command);
}

public static String exec(long timeout, List<String> command) throws IOException, InterruptedException {
    for (String string : command) {
        System.out.print(string + " ");
    }
    System.out.println();

    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);

    Process process = pb.start();
    AtomicReference<String> output = new AtomicReference<>();
    Thread thread = new Thread(() -> {
        try (Reader reader = new InputStreamReader(process.getInputStream())) {
            output.set(IOUtils.toString(reader));
        } catch (IOException e) {
            log.warn("执行结果失败", e);
        }
    });
    thread.setDaemon(true);
    thread.start();

    process.waitFor(timeout, TimeUnit.MILLISECONDS);
    try {
        process.exitValue();
    } catch (IllegalThreadStateException e) {
        process.destroy();
    }

    return output.get();
}

`

运行结果:
image

@uhaorph1636
Copy link
Author

问题解决了 我在最外层使用了一个shell脚本 可以使用了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant