360精选
BASE64,是一种公共编解码算法 大家都是遵循同一种算法来对字节数组进行编解码,所以可以通用. java在很多公共的框架中都有BASE64的实现. 可以用: Apache Commons Codec 的实现 !-- - dependency groupIdcommons-codec/groupId artifactIdcommons-codec/artifactId version1.10/version /dependency 当然,也可以自己找一个实现类,自己实现一个工具类即可. 以下是从开源jar包中提取的工具类 /* * $Id: dea5898ba414326f92242c84d08b7285ecd0589c $ * * This file is part of the iText (R) project. * Copyright (c) 1998-2016 iText Group NV * Authors: Bruno Lowagie, Paulo Soares, et al. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License version 3 * as published by the Free Software Foundation with the addition of the * following permission added to Section 15 as permitted in Section 7(a): * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY * ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT * OF THIRD PARTY RIGHTS * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License * along with this program; if not, see or write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA, 02110-1301 USA, or download the license from the following URL: * * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU Affero General Public License. * * In accordance with Section 7(b) of the GNU Affero General Public License, * a covered work must retain the producer line in every PDF that is created * or manipulated using iText. * * You can be released from the requirements of the license by purchasing * a commercial license. Buying such a license is mandatory as soon as you * develop commercial activities involving the iText software without * disclosing the source code of your own applications. * These activities include: offering paid services to customers as an ASP, * serving PDFs on the fly in a web application, shipping iText with a closed * source product. * * For more information, please contact iText Software Corp. at this * address: sales@itextpdf.com */ /* * The original version of this file was placed in the public domain * by Robert Harder. */ package enumtest; /** * pEncodes and decodes to and from Base64 notation./p * pHomepage: a href= * * p * Change Log: * /p * ul * liv2.2.1 - Fixed bug using URL_SAFE and ORDERED encodings. Fixed bug * when using very small files (~ 40 bytes)./li * liv2.2 - Added some helper methods for encoding/decoding directly from * one file to the next. Also added a main() method to support command line * encoding/decoding from one file to the next. Also added these Base64 dialects: * ol * liThe default is RFC3548 format./li * liCalling Base64.setFormat(Base64.BASE64_FORMAT.URLSAFE_FORMAT) generates * URL and file name friendly format as described in Section 4 of RFC3548. * * liCalling Base64.setFormat(Base64.BASE64_FORMAT.ORDERED_FORMAT) generates * URL and file name friendly format that preserves lexical ordering as described * in * /ol * Special thanks to Jim Kellerman at a href= * for contributing the new Base64 dialects. * /li * * liv2.1 - Cleaned up javadoc comments and unused variables and methods. Added * some convenience methods for reading and writing to and from files./li * liv2.0.2 - Now specifies UTF-8 encoding in places where the code fails on systems * with other encodings (like EBCDIC)./li * liv2.0.1 - Fixed an error when decoding a single byte, that is, when the * encoded data was a single byte./li * liv2.0 - I got rid of methods that used booleans to set options. * Now everything is more consolidated and cleaner. The code now detects * when data that's being decoded is gzip-compressed and will decompress it * automatically. Generally things are cleaner. You'll probably have to * change some method calls that you were making to support the new * options format (ttint/tts that you OR together)./li * liv1.5.1 - Fixed bug when decompressing and decoding to a * byte[] using ttdecode( String s, boolean gzipCompressed )/tt. * Added the ability to suspend encoding in the Output Stream so * you can turn on and off the encoding if you need to embed base64 * data in an otherwise normal stream (like an XML file)./li * liv1.5 - Output stream pases on flush() command but doesn't do anything itself. * This helps when using GZIP streams. * Added the ability to GZip-compress objects before encoding them./li * liv1.4 - Added helper methods to read/write files./li * liv1.3.6 - Fixed OutputStream.flush() so that 'position' is reset./li * liv1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream * where last buffer being read, if not completely full, was not returned./li * liv1.3.4 - Fixed when improperly padded stream error was thrown at the wrong time./li * liv1.3.3 - Fixed I/O streams which were totally messed up./li * /ul * * p * I am placing this code in the Public Domain. Do with it as you will. * This software comes with no guarantees or warranties but with * plenty of well-wishing instead! * Please visit a href= * periodically to check for updates or to contribute improvements. * /p * * @author Robert Harder * @author rob@iharder.net * @version 2.2.1 */ public class Base64 { /* ** P U B L I C F I E L D S *** / /** No options specified. Value is zero. */ public final static int NO_OPTIONS = 0; /** Specify encoding. */ public final static int ENCODE = 1; /** Specify decoding. */ public final static int DECODE = 0; /** Specify that data should be gzip-compressed. */ public final static int GZIP = 2; /** Don't break lines when encoding (violates strict Base64 specification) */ public final static int DONT_BREAK_LINES = 8; /** * Encode using Base64-like encoding that is URL- and Filename-safe as described * in Section 4 of RFC3548: * a href= * It is important to note that data encoded this way is emnot/em officially valid Base64, * or at the very least should not be called Base64 without also specifying that is * was encoded using the URL- and Filename-safe dialect. */ public final static int URL_SAFE = 16; /** * Encode using the special ordered dialect of Base64 described here: * a href= */ public final static int ORDERED = 32; /* ** P R I V A T E F I E L D S *** / /** Maximum line length (76) of Base64 output. */ private final static int MAX_LINE_LENGTH = 76; /** The equals sign (=) as a byte. */ private final static byte EQUALS_SIGN = (byte)'='; /** The new line character (\n) as a byte. */ private final static byte NEW_LINE = (byte)'\n'; /** Preferred encoding. */ private final static String PREFERRED_ENCODING = UTF-8; // I think I end up not using the BAD_ENCODING indicator. //private final static byte BAD_ENCODING = -9; // Indicates error in encoding private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding /* ** S T A N D A R D B A S E 6 4 A L P H A B E T *** / /** The 64 valid Base64 values. */ //private final static byte[] ALPHABET; /* Host platform me be something funny like EBCDIC, so we
查看更多

【图】C 将文件进行Base64转码解码

360图片
没有更多结果了~