为什么 Java 中 byte[] 的 Base64 编码不起作用?
Why doesn't Base64 Encoding of a byte[] in Java work?
import java.io.*;
import java.nio.*;
import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;
public class Abc {
public static String readFileAsString(String filePath) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
try {
long len = new java.io.File(filePath).length();
if (len > Integer.MAX_VALUE) throw new IOException("File " + filePath + " too large")
byte[] bytes = new byte[(int) len];
dis.readFully(bytes);
String ans = new String(bytes, "UTF-8");
return ans;
} finally {
dis.close();
}
}
public static void main(String args[]) throws IOException {
String base64encodedString = null;
FileOutputStream stream = new FileOutputStream("C:\Users\EMP142738\Desktop\New folder\Readhjbdsdsefd.pdf");
String filePath = new String("C:\Users\EMP142738\Desktop\New folder\Readers Quick Ref Card.pdf");
try {
base64encodedString = java.util.Base64.getUrlEncoder().encodeToString(new Abc().readFileAsString(filePath).getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
try {
byte[] base64decodedBytes = java.util.Base64.getUrlDecoder().decode(base64encodedString);
stream.write(base64decodedBytes);
} catch(IOException e){
e.printStackTrace();}
finally {
stream.close();
}//catch (FileNotFoundException e) {
// e.printStackTrace();
}
}
我正在尝试使用 Base64 对 PDF 文件进行编码和解码。我正在做的是将 PDF(二进制文件)转换为 ByteArray,然后将 ByteArray 作为字符串返回。然后我使用 java.util.Base64 在 Base64 中对这个字符串进行编码。当我尝试回溯整个过程时,我能够转换 PDF(二进制文件),但文件是 corrupted/damaged。此外,整个过程(编码-解码)后的输出文件明显大于输入文件。我希望它们的大小相同。我在这里做错了什么?
编辑 1(2016 年 7 月 13 日):
在 main 方法中,我根据 Jim 的建议修改了代码。
在阅读了相同的文档后,我尝试使用 Base64.encode(byte[] src) 。但是它一直给出错误 "cannot find symbol Base64.encode(byte[])"。但是我使用了相同 Class(java.util.Base64.Encoder) 中的 encodetoString 方法。我无法理解这里的问题。
这是从 readFileAsString 方法返回 byte[] 后使用的修改后的 main 方法。
public void main(String args[]) throws IOException {
String filePath = new String("C:\Users\EMP142738\Desktop\New folder\Readers Quick Ref Card.pdf");
byte[] src = new Abc().readFileAsString(filePath);
byte[] destination = Base64.encode(src);
}
问题出在你的流程上
byte[] -> String -> base64 string
需要省略转String
,直接走:
byte[] -> base64 string
转换为字符串会破坏二进制流,因为它涉及从输入字符集到 16 位 Unicode 字符的解码操作。
import java.io.*;
import java.nio.*;
import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;
public class Abc {
public static String readFileAsString(String filePath) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
try {
long len = new java.io.File(filePath).length();
if (len > Integer.MAX_VALUE) throw new IOException("File " + filePath + " too large")
byte[] bytes = new byte[(int) len];
dis.readFully(bytes);
String ans = new String(bytes, "UTF-8");
return ans;
} finally {
dis.close();
}
}
public static void main(String args[]) throws IOException {
String base64encodedString = null;
FileOutputStream stream = new FileOutputStream("C:\Users\EMP142738\Desktop\New folder\Readhjbdsdsefd.pdf");
String filePath = new String("C:\Users\EMP142738\Desktop\New folder\Readers Quick Ref Card.pdf");
try {
base64encodedString = java.util.Base64.getUrlEncoder().encodeToString(new Abc().readFileAsString(filePath).getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
try {
byte[] base64decodedBytes = java.util.Base64.getUrlDecoder().decode(base64encodedString);
stream.write(base64decodedBytes);
} catch(IOException e){
e.printStackTrace();}
finally {
stream.close();
}//catch (FileNotFoundException e) {
// e.printStackTrace();
}
}
我正在尝试使用 Base64 对 PDF 文件进行编码和解码。我正在做的是将 PDF(二进制文件)转换为 ByteArray,然后将 ByteArray 作为字符串返回。然后我使用 java.util.Base64 在 Base64 中对这个字符串进行编码。当我尝试回溯整个过程时,我能够转换 PDF(二进制文件),但文件是 corrupted/damaged。此外,整个过程(编码-解码)后的输出文件明显大于输入文件。我希望它们的大小相同。我在这里做错了什么?
编辑 1(2016 年 7 月 13 日): 在 main 方法中,我根据 Jim 的建议修改了代码。 在阅读了相同的文档后,我尝试使用 Base64.encode(byte[] src) 。但是它一直给出错误 "cannot find symbol Base64.encode(byte[])"。但是我使用了相同 Class(java.util.Base64.Encoder) 中的 encodetoString 方法。我无法理解这里的问题。 这是从 readFileAsString 方法返回 byte[] 后使用的修改后的 main 方法。
public void main(String args[]) throws IOException {
String filePath = new String("C:\Users\EMP142738\Desktop\New folder\Readers Quick Ref Card.pdf");
byte[] src = new Abc().readFileAsString(filePath);
byte[] destination = Base64.encode(src);
}
问题出在你的流程上
byte[] -> String -> base64 string
需要省略转String
,直接走:
byte[] -> base64 string
转换为字符串会破坏二进制流,因为它涉及从输入字符集到 16 位 Unicode 字符的解码操作。