0
嗨即时解码base64字符串数据并保存到像url格式的数据库与时间戳,所以我需要压缩解码的数据和我的压缩,如果未来的数据小于100kb然后不需要压缩,否则将数据压缩到50%的折扣。将base64编码的字符串数据压缩为解码格式
try
{
String FileItemRefPath = propsFPCConfig.getProperty("fileCreationReferencePath");
String imageURLReferncePath = propsFPCConfig.getProperty("imageURLReferncePath");
File f = new File(FileItemRefPath+"/"+"productimages"+"/"+donorId);
String strException = "Actual File "+f.getName();
if(!f.exists())
{
if (!f.mkdirs())
{
System.out.println("direction creation failed");
return null;
}
}
else
{
boolean isdirCreationStatus = f.mkdirs();
}
String strDateTobeAppended = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
String fileName = strImageName+strDateTobeAppended;
savedFile = new File(f.getAbsolutePath()+"/"+fileName);
strException=strException+" savedFile "+savedFile.getName();
Base64 decoder = new Base64();
byte[] decodedBytes = decoder.decode(strImageBase64);
if((decodedBytes != null) && (decodedBytes.length != 0))
{
System.out.println("Decoded bytes length:"+decodedBytes.length);
fos = new FileOutputStream(savedFile);
System.out.println(new String(decodedBytes) + "\n") ;
int x=0;
{
fos.write(decodedBytes, 0, decodedBytes.length);
}
fos.flush();
}
if(fos != null)
{
fos.close();
System.out.println("file output stream"+savedFile.getName());
return savedFile.getName();
}
else
{
return null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(fos!= null)
{
fos.close();
}
else
{
savedFile = null;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return savedFile.getName();
}
我不太确定你的问题在这里。您无法事先确定给定的八位字节流可以压缩多少。如果你想压缩数据,你总是可以使用LZMA算法。有一个纯Java实现它的地方。去谷歌上查询。 :) –
对不起老板,这里即时获取base64字符串数据,所以它是> 100 kb,所以我想压缩数据 – user2964861
那么,为什么你base64的数据编码?这增加了33%的开销。如果你压缩这个,结果将不再是base64编码,你基本上回到原点。你想要准确表达什么? –