我正在创建Image for base64转换的演示应用程序。一些图像正确转换base64。当图像大小为3.92Mb(4128 * 3096)并成功转换为base64并上传到服务器但未上传。 在Base64字符串写入文件和文本文件大小为5.40Mb。
Code For Image Convet to base64
public String convertImage(File file) {
FileInputStream inputSteam = new FileInputStream(file);
inputSteam.read(getBytesFromFile(file));
return Base64.encodeToString(getBytesFromFile(file, Base64.DEFAULT);
}
public byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+ file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
如何处理此问题。或任何其他转换图像的方式;
答案 0 :(得分:0)
如果您使用PHP作为服务器,您是否在PHP.ini中检查了上传文件的最大大小
答案 1 :(得分:0)
base64字符串文件应该大于原始文件大小,因为base64编码在字符串中添加自己的字符以维护base64格式。这可能是因为文件大小比原始大小更大的问题。