문제가 발생한 이유 : 누군가가 이미 지정한대로 :
byte []로 시작하고 실제로 텍스트 데이터를 포함하지 않으면 "적절한 변환"이 없습니다. 문자열은 텍스트를위한 것이고 byte []는 이진 데이터를위한 것이며, 실제로 할 수있는 유일한 방법은 반드시 필요한 경우가 아니라면 문자열 간 변환을 피하는 것입니다.
pdf 파일에서 byte []를 만든 다음 String으로 변환 한 다음 String을 입력으로 사용하고 파일로 다시 변환하려고 할 때이 문제가 관찰되었습니다.
따라서 인코딩 및 디코딩 논리가 내가했던 것과 동일한 지 확인하십시오. 바이트 []를 Base64로 명시 적으로 인코딩하고 파일을 다시 생성하기 위해 디코딩했습니다.
사용 사례 :
일부 제한으로 인해 전송 byte[]을 시도 request(POST)했으며 프로세스는 다음과 같습니다.
PDF 파일 >> Base64.encodeBase64 (byte []) >> 문자열 >> 요청 전송 (POST) >> 수신 문자열 >> Base64.decodeBase64 (byte []) >> 바이너리 생성
이것을 시도하고 이것은 나를 위해 일했다 ..
File file = new File("filePath");
byte[] byteArray = new byte[(int) file.length()];
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(byteArray);
String byteArrayStr= new String(Base64.encodeBase64(byteArray));
FileOutputStream fos = new FileOutputStream("newFilePath");
fos.write(Base64.decodeBase64(byteArrayStr.getBytes()));
fos.close();
}
catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
}
catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
byte[]이진 데이터와String텍스트에 a 를 사용하면 무엇이 문제입니까 ?