开发者

Java: PDF converter works in Mac but in Windows, produce empty PDF file

I need a little help here with my PDF converter program.

So, I'm doing this mobile agent PDF converter using JADE framework. But, the problem that I am facing is more related to the way I convert a text file into PDF, send it across network as binary and restore PDF file back.

The program that I've written works properly on my MacBook. But, on a Windows, it restores my PDF file as an empty PDF.

Here is my code that I use for sending the PDF file across.

private void sendPDF(File f, String recipient) {
    String content = "";

    if(f != null) {
        try {
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            int noBytesRead = 0;
            byte[] buffer = new byte[1024];

            while((noBytesRead = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, noBytesRead);
            }

            content = baos.toString();
            fis.close();
            baos.close();

            System.out.println("Successful PDF-to-byte conversion.");
        } catch (Exception e) {
            System.out.println("Exception while converting PDF-to-byte.");
            content = "failed";
            e.printStackTrace();
        }
    } else {
        System.out.println("PDF-to-file conversion failed.");
        content = "failed";
    }

    ACLMessage message = new ACLMessage(ACLMessage.INFORM);
    message.addReceiver(new AID(recipient, AID.ISLOCALNAME));
    message.setContent(content);

    myAgent.send(message);
    System.out.println("PDF document has been sent to requesting client.");
}

And, here is the code that I use to restore the PDF back.

private File restorePDF(String content) {
    String dirPDF = dirBuffer + "/" + new Date().getTime() + ".pdf";
    File f = new File(dirPDF);

    try {
        if(!f.exists()) f.createNewFile();

      开发者_Go百科  byte[] buffer = new byte[1024];
        ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
            FileOutputStream fos = new FileOutputStream(f);

        int noBytesRead = 0;
        while((noBytesRead = bais.read(buffer)) != -1) {
                fos.write(buffer, 0, noBytesRead);
    }

        fos.flush();
        fos.close();
        bais.close();
    } catch (Exception e) {
        e.printStackTrace();
        f = null;
    }

    return f;
}

Any help on this would be much appreciated! :)


Question is a little bit confusing, since there is nothing specific about PDF content.

I am supposing you actually want to send bytes, actually send a string, and the string encoding is different on the client and server.

This is usually where troubles happen:

 content = baos.toString();

and:

 content.getBytes()


A PDF file is a binary file format with lookup tables and lots of binary data blocks to making it a String will break it. If you want to know about the insides of a PDF file, I have written a whole load of blog posts about it (http://www.jpedal.org/PDFblog/2010/09/understanding-the-pdf-file-format-series/)


One issue is that you're using the wrong separator char. Java has a built in function that will return the correct char for the correct os. See separator char.

Your code will look something like this

String dirPDF = dirBuffer + File.separatorChar + new Date().getTime() + ".pdf";

For reference:

separatorChar

The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜