gpt4 book ai didi

java - 尝试从 java 运行 gsutils 永远不会返回

转载 作者:行者123 更新时间:2023-12-02 03:23:21 25 4
gpt4 key购买 nike

我尝试从云端的 Google 存储下载文件夹。

我从具有权限的用户进程中运行(当我从 Mac 上的常规终端运行时,它可以工作)

我有这个代码:

public void runCommand() {
final Process p;
try {
p = Runtime.getRuntime().exec(
"gsutil -m cp -r gs://my_bucket/705/201609040613/output/html_pages file:/Users/eladb/WorkspaceQa/GsClient/build/resources/main/downloads/");

new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;

try {
while ((line = input.readLine()) != null)
System.out.println(line);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}

但是新线程永远不会返回。

卡在线上:

while ((line = input.readLine()) != null)

有什么方法可以从谷歌云下载这些文件夹吗?

最佳答案

这种手动复制 stdout 数据很容易出错(您必须强制关闭流才能终止子线程),值得庆幸的是,unnecessary since Java 7 :

public void runCommand() {
try {
new ProcessBuilder("gsutil", "-m", "cp", "-r",
"gs://my_bucket/705/201609040613/output/html_pages",
"file:/Users/eladb/WorkspaceQa/GsClient/build/resources/main/downloads/")
.inheritIO()
.start()
.waitFor();
} catch(IOException | InterruptedException e) {
e.printStackTrace();
}
}

如果您不想以这种方式引导所有三个 channel ,请参阅 redirectOutput(File) , redirectOutput(ProcessBuilder.Redirect) ,以及输入和错误 channel 的类似方法。

<小时/>

仅限(默认)模式 ProcessBuilder.Redirect.PIPE要求您在子流程运行时提供输入或接收输出。

关于java - 尝试从 java 运行 gsutils 永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com