我无法在FOP过程中从Java捕获控制台输出,并且我的所有过程都无法正常工作

时间:2014-09-19 10:02:28

标签: java xml xslt console apache-fop

我正在使用eclipse + java。我有一个从控制台执行指令的方法。复制&从互联网上粘贴......

public void viewPDF(String cmd){

    try {

        Process p = Runtime.getRuntime().exec( cmd);

        BufferedReader in = new BufferedReader(  
                            new InputStreamReader(p.getInputStream()));  
        String line = null;  
        while ((line = in.readLine()) != null) {  
            System.out.println(line);  
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如果我在console cmd中执行此代码,则一切正常。

C:\fop-0.93\fop -xml C:\document.xml -xsl C:\document.xsl -pdf C:\document.pdf -d

但如果我尝试从java方法执行它不起作用,因为我无法捕获控制台输出。

控制台正在等待,最后我需要使用红色按钮停止......

有人可以捕获控制台输出吗?与其他进程我可以(.bat,文件读取等...)但捕获fop输出是不可能的。

PD:抱歉我的英文...... :(

感谢!!!

1 个答案:

答案 0 :(得分:0)

最后,我没有从控制台运行fop并捕获其输出,而是创建了一个创建pdf的java程序。

这篇文章中有人:

Fop exception when FopFactory.newInstance()

FWIW,FOP只是一个java库。您不希望将其作为外部进程执行,只需使用STI API即可。例如,请参阅code.google.com/p/fop-maven-plugin/source/browse / ...,了解如何解决这个问题。 - Gyro Gearless 8月30日21:27

这是我创建的课程#34;受到启发":D其他:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class ConstructorFOP {

    private InputStream inputStream;

    public void xmlToPdf(String RUTA_XML, String RUTA_XSL, String RUTA_PDF) {

        try {
            System.out.println("Preparing...");

            // Setup directories
            File baseDir = new File("C:");
            File outDir = new File(baseDir, "out");
            outDir.mkdirs();

            File xmlfile = new File(RUTA_XML);
            File xsltfile = new File(RUTA_XSL);
            File pdffile = new File(RUTA_PDF);

            System.out.println("Input XML : " + xmlfile);
            System.out.println("Input XSL : " + xsltfile);
            System.out.println("OutputPDF : " + pdffile);
            System.out.println("");
            System.out.println("Transforming...");

            // configure fopFactory as desired
            final FopFactory fopFactory = FopFactory.newInstance();

            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            // configure foUserAgent as desired

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            TransformerFactory tFactory = TransformerFactory.newInstance();

            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup input
            Source src = new StreamSource(xmlfile);

            // Setup Transformer
            Source xsltSrc = new StreamSource(xsltfile);
            Transformer transformer = tFactory.newTransformer(xsltSrc);

            // Make sure the XSL transformation's result is piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(src, res);

            ByteArrayOutputStream baos = out;
            inputStream = new ByteArrayInputStream(baos.toByteArray());

            InputStreamAFile(pdffile, inputStream);
        } catch (FOPException e) {
            System.out.println("ERROR DOC 3");
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            System.out.println("ERROR DOC 4");
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            System.out.println("ERROR DOC 5");
            e.printStackTrace();
        } catch (TransformerException e) {
            System.out.println("ERROR DOC 6");
            e.printStackTrace();
        }
    }

    public void InputStreamAFile(File pdffile, InputStream entrada) {
        try {
            OutputStream salida = new FileOutputStream(pdffile);
            byte[] buf = new byte[1024];// Actualizado me olvide del 1024
            int len;
            while ((len = entrada.read(buf)) > 0) {
                salida.write(buf, 0, len);
            }
            salida.close();
            entrada.close();
            System.out.println("Se realizo la conversion con exito");
        } catch (FileNotFoundException e) {
            System.out.println("ERROR DOC 1");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("ERROR DOC 2");
            e.printStackTrace();
        }
    }
}

感谢!!!