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

Java 将pdf转换为byteArray-byteArray转换为String-String转换为byteArray-byteArray转换为pdf,java,arrays,pdf,Java,Arrays,Pdf,下面的代码适用于文本文件,但不适用于pdf文件。我的文件包含英文和希腊文字符。我尝试将pdf文件转换为byteStream格式,将byteStream格式转换为String格式,以便将其保存在数据库中。在此之后,我尝试从保存的字符串创建pdf 有什么帮助吗 public class PdfToByteStream { public static byte[] convertDocToByteArray(String path)throws FileNotFoundException,

下面的代码适用于文本文件,但不适用于
pdf
文件。我的文件包含英文和希腊文字符。我尝试将
pdf
文件转换为
byteStream
格式,将
byteStream
格式转换为
String
格式,以便将其保存在
数据库中。在此之后,我尝试从保存的字符串创建
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);
    }
}
如果使用编码,您将能够将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);
}

放学后在黑板上写100遍:“
String
不是二进制数据的容器。”。你节省了我的时间!