如何使用文件路径中的空格从java调用ghostscript [英] How to call ghostscript from java with whitespaces in file paths
问题描述
最近我发现自己不得不在java和windows环境中启动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被取代为gswin32在Windows上,因为ghostscript是在路径。
我很快意识到我不得不以某种方式逃避文件名,所以我做的第一件事是将它们包含在双引号之间。这在Windows上工作,但不是在linux上:在linux上我已经尝试了双引号,并且使用反斜杠转义了空格,但没有成功。
要启动命令,我使用
Runtime.getRuntime()。exec(command);
单串。我发现以下问题获取ghostscript以其名称中的空格(例如我的文档)中的文件,但是:
- 我希望将其扩展为linux;
- 我发现双引号适用于我,与其中指出的不同。
我想了解这个东西一次:你能帮我做这个吗?
下面是我的总结
Windows
以双引号括起文件名为我工作:
gswin32 -q -dNOPAUSE -dBATCH -sDEVICE = pnggray -r300 -sOutputFile =C:\Program Files\tomcat 6.0.33 with space \temp\Thread-11\img-%03d.pngC:\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
p>
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
为什么不使用运行时。 exec(String [] args))它需要多个参数?此变体旨在避免您必须逃避此类参数。由于参数是单独提供的,因此不需要基于空格的插值,因此不会有任何混淆。
Recently I found myself having to launch the ghostscript command from java, in both linux and windows environments, with whitespaces in input/output filenames. An example of the command follows:
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
The gs gets replaced by gswin32 on windows, given that ghostscript is in the Path.
I quickly realized that I had to escape the file names in some manner, so the first thing I've done was to enclose them between double quotes. This worked on windows, but not on linux: on linux I've tried the double quotes enclosing and also escaping whitespaces with backslashes, but without success.
For launching the command I'm using Runtime.getRuntime().exec(command);
, passing one single string. I found the following question getting ghostscript to take in files with spaces in their name (like something in "my documents") but:
- I desired to extend it also for linux;
- I found that double quoting works for me, differently than how it's pointed out there.
I would like to understand this thing once per all: can you help me to do this?
Here follows a summary of my attempts, per SO.
Windows
Enclosing file names in double quotes worked for me:
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
Tried to enclose file names in double quotes
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"
Tried to escape white space with backslash
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
Tried both together
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"
Why don't you use Runtime.exec(String[] args) which takes multiple arguments ? This variant is designed to avoid you having to escape such arguments. Since the arguments are provided separately there's no space-based interpolation required and thus no confusion.
这篇关于如何使用文件路径中的空格从java调用ghostscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!