我有以下代码适用于文本文件,但不适用于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);
}
}
答案 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);
}