【发布时间】:2012-12-10 11:04:11
【问题描述】:
最近,我发现自己必须在 linux 和 windows 环境中从 java 启动 ghostscript 命令,输入/输出文件名中有空格。命令示例如下:
gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile=/home/nic/tomcat/6.0.33 with spaces/temp/Thread-11/img-%03d.png /home/nic/tomcat/6.0.33 with spaces/temp/tmpfile.tmp
gs 在 Windows 上被 gswin32 替换,因为 ghostscript 在路径中。
我很快意识到我必须以某种方式对文件名进行转义,所以我做的第一件事就是用双引号将它们括起来。这适用于 Windows,但不适用于 linux:在 linux 上,我尝试使用双引号括起来并用反斜杠转义空格,但没有成功。
为了启动我正在使用的命令Runtime.getRuntime().exec(command);,传递一个字符串。我发现了以下问题getting ghostscript to take in files with spaces in their name (like something in "my documents") 但是:
- 我也想为 linux 扩展它;
- 我发现双引号对我有用,与那里指出的方式不同。
我想彻底理解这件事:你能帮我做这件事吗?
根据 SO,以下是我的尝试总结。
窗口
用双引号括起来的文件名对我有用:
gswin32 -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="C:\Program Files\tomcat 6.0.33 with spaces\temp\Thread-11\img-%03d.png" "C:\Program Files\tomcat 6.0.33 with spaces\temp\tmpfile.tmp"
Linux
试图用双引号将文件名括起来
gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="/home/nic/tomcat/6.0.33 with spaces/temp/Thread-11/img-%03d.png" "/home/nic/tomcat/6.0.33 with spaces/temp/tmpfile.tmp"
试图用反斜杠转义空格
gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile=/home/nic/tomcat/6.0.33\ with\ spaces/temp/Thread-11/img-%03d.png /home/nic/tomcat/6.0.33\ with\ spaces/temp/tmpfile.tmp
两个都试了
gs -q -dNOPAUSE -dBATCH -sDEVICE=pnggray -r300 -sOutputFile="/home/nic/tomcat/6.0.33\ with\ spaces/temp/Thread-11/img-%03d.png" "/home/nic/tomcat/6.0.33\ with\ spaces/temp/tmpfile.tmp"
【问题讨论】:
标签: java escaping whitespace ghostscript