将pdf转换为byteArray - byteArray转换为String - String转换为byteArray - byteArray转换为pdf

时间:2017-11-13 09:02:30

标签: java arrays pdf

我有以下代码适用于文本文件,但不适用于pdf个文件。我的文件包含英文和希腊文字符。我尝试将pdf文件转换为byteStream,将byteStream格式转换为String格式,以便将其保存在database中。在此之后,我尝试从保存的字符串中创建pdf

任何帮助?

public class PdfToByteStream {

    public static byte[] convertDocToByteArray(String path)throws FileNotFoundException, IOException{
        File file = new File(path);

        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }
        } catch (IOException ex) {
            Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    }

    public static void convertByteArrayToDoc(String path, byte[] bytes)throws FileNotFoundException, IOException {
        File someFile = new File(path);
        FileOutputStream fos = new FileOutputStream(someFile);
        fos.write(bytes);
        fos.flush();
        fos.close();
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        byte[] bytes = convertDocToByteArray("path/test.pdf");

        String stream = new String(bytes, "UTF-8");//ok for txt
        byte[] newBytes = stream.getBytes(Charset.forName("UTF-8")); // ok for txt

        convertByteArrayToDoc("path/newTest.pdf", newBytes);
    }
}

1 个答案:

答案 0 :(得分:1)

如果您使用Base64编码,则可以将PDF转换为字符串并返回。

以下是需要更改的代码的相关部分:

import java.util.Base64;

...

public static void main(String[] args) throws FileNotFoundException, IOException {
    byte[] bytes = convertDocToByteArray("some.pdf");
    String stream = Base64.getEncoder().encodeToString(bytes);
    byte[] newBytes = Base64.getDecoder().decode(stream);
    convertByteArrayToDoc("some_new.pdf", newBytes);
}