java - 将base64编码的字符串数据压缩成decode格式

标签 java web-services tomcat

您好,我正在解码 base64 字符串数据并像带有时间戳的 url 格式一样保存到数据库中,所以我需要压缩解码数据和我的压缩,如果即将到来的数据小于 100kb,则无需压缩,否则压缩数据享受 50% 的折扣。

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();
}

最佳答案

您的主要问题可能不是用 Java 压缩它。为此,您可以简单地使用类似 the Deflater class 的东西.然而,在 iOS 上,它可能会稍微复杂一些,因为我不确定您在 iOS 中可以使用哪种类似 zlib 的工具。

关于java - 将base64编码的字符串数据压缩成decode格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827877/

相关文章:

java - 在应用程序服务器上部署 Web 应用程序。数据库访问凭证

java - 如何更改 GWT 中 DecoratorPanel 的颜色?

java - JDBC+SSL : Bundling a CA cert, 客户端证书和客户端到单个 keystore 文件中

java - Android 中 SparseArrayCompat 政策不断增长背后的基本原理是什么?

java - Tomcat 一直要求输入密码和用户名

java - 部署 servlet 时遇到问题。(intellij ultimate)

java - Android更改警报声音

c# - 将 XML 发布到 REST 时出现 HTTP 401 错误

web-services - 是否有类似于亚马逊 AWS 的可扩展电子邮件服务用于存储?

javascript - 在美国以外是否有获取潮汐信息的网络服务?