다음 코드는 텍스트 파일에서는 작동하지만 pdf
파일에서는 작동하지 않습니다. 내 파일에 영어와 그리스 문자가 포함되어 있습니다. pdf
파일을 byteStream
으로 변환하고 byteStream
을 String
형식으로 변환하여 database
에 저장하려고합니다. 이 후 저장된 문자열에서 pdf
을 만들려고합니다.pdf to byteArray - byteArray to String - byteArray - byteArray to 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);
}
}
https://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences 방과 후 칠판 100 배에 – JimmyB
쓰기 : '문자열'은 바이너리 데이터의 컨테이너가 아닙니다. ' – EJP