简体   繁体   English

从Java Servlet执行时Imagemagick无法打开文件

[英]Imagemagick can't open file when executed from Java Servlet

I'm trying to convert files from png's to pdf using imagemagick and Java. 我正在尝试使用imagemagick和Java将文件从png转换为pdf。 I've got everything working to a place when I'm executing imagemagick command to actually merge multiple png's into one pdf. 当我执行imagemagick命令将多个png实际合并为一个pdf时,我已经将所有工作正常工作了。 The command itself looks properly, and it works fine when executed in the terminal but my application gives me error showing that imgck can't open the file (even though it exists and I've set permissions to the folder to 777 : 该命令本身看起来正确,并且在终端中执行时可以正常工作,但是我的应用程序给我显示了imgck无法打开文件的错误(即使它存在并且我已将该文件夹的权限设置为777):

line: convert: unable to open image `"/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001005.png"': No such file or directory @ error/blob.c/OpenBlob/2642.

This is my command : 这是我的命令:

/opt/local/bin/convert "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001005.png" "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001219.png" "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/complete-exportedPanel2013-01-1003:13:17.212.pdf"

And my Java code : 而我的Java代码:

String filesString = "";

for (String s : pdfs){
    filesString += "\""+ s + "\" ";
}

Process imgkProcess = null;

BufferedReader br = null;
File f1 = new File(pdfs[0]);

//returns true
System.out.println("OE: "+f1.exists());

String cmd = imgkPath+"convert "+ filesString+ " \""+outputPath+outName+"\"";
try {
    imgkProcess = Runtime.getRuntime().exec(cmd);
    InputStream stderr = imgkProcess.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    br = new BufferedReader(isr);
} catch (IOException e1) {
    msg = e1.getMessage(); 
}

imgkProcess.waitFor();

while( (line=br.readLine() ) != null){
    System.out.println("line: "+line);
}

The whole code is executed from a java servlet controller after getting request from a form. 从表单获取请求后,整个代码从Java Servlet控制器执行。 Any ideas what can cause this ? 有什么想法会导致这种情况吗? I'm using latest imgck, jdk, and osx 10.7 . 我正在使用最新的imgck,jdk和osx 10.7。

A few things: 一些东西:

  • When spawning anything but really trivial processes, it's usually better to use ProcessBuilder than Runtime.exec() - it gives you much better control 当生成除真正琐碎的进程之外的任何东西时,通常使用ProcessBuilder比使用Runtime.exec()更好-它为您提供了更好的控制

  • Even with ProcessBuilder, it often works better to write a shell script that does what you need. 即使使用ProcessBuilder,编写满足您需要的shell脚本通常也更好。 Then spawn a process to run the script. 然后产生一个进程来运行脚本。 You get a lot more control in shell script than you do in ProcessBuilder 与在ProcessBuilder中相比,您在Shell脚本中获得了更多的控制权

  • Remember that a spawned process is not a shell. 请记住,产生的进程不是外壳程序。 It can't, for instance, evaluate expressions, or expand shell variables. 例如,它不能评估表达式或扩展shell变量。 If you need that, then you must execute a shell (like sh or bash). 如果需要,则必须执行一个shell(如sh或bash)。 Better yet, write a shell script as described above 更好的是,如上所述编写一个shell脚本

  • If all you need to do is to execute some ImageMagick commands, it would probably be easier to jmagick, a Java interface to ImageMagick - see http://www.jmagick.org/ 如果您所需要做的只是执行一些ImageMagick命令,那么jmagick(ImageMagick的Java接口)可能会更容易-请访问http://www.jmagick.org/

  • Actually, since the you're assembling images into a PDF, the iText library - http://itextpdf.com is probably the best tool for the job, as it is native Java code, does not require spawning a native process, and will therefore be much more portable. 实际上,由于您是将图像组合成PDF,因此iText库-http: //itextpdf.com可能是完成此工作的最佳工具,因为它是本机Java代码,不需要生成本机进程,并且会因此更加便携。

Solved it by adding all arguments to an arrayList and then casting it to String array. 通过将所有参数添加到arrayList并将其强制转换为String数组来解决此问题。

ArrayList<String> cmd = new ArrayList<String>();
cmd.add(imgkPath+"convert");

for (int i=0, l=pdfs.length; i<l; i++){
    cmd.add(pdfs[i]);
}
cmd.add(outputPath+outName);

imgkProcess = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM