java 序列化base64方式示例

代码示例:

public static Object readObject(String string)
	{
		ByteArrayInputStream bins = null;
		ObjectInputStream ins = null;

		try
		{
			byte[] buff = new BASE64Decoder().decodeBuffer(string);
			bins = new ByteArrayInputStream(buff);
			ins = new ObjectInputStream(bins);

			ins.close();

			return ins.readObject();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try
			{
				if (null != bins)
				{
					bins.close();
				}
			}
			catch (IOException ex)
			{
			}
			try
			{
				if (null != ins)
				{
					ins.close();
				}
			}
			catch (IOException ex)
			{
			}
		}
	}

	public static String object2String(Serializable obj)
	{
		ObjectOutputStream out = null;
		ByteArrayOutputStream byteStream = null;
		try
		{
			byteStream = new ByteArrayOutputStream();
			out = new ObjectOutputStream(byteStream);
			out.writeObject(obj);
			byte[] buff = byteStream.toByteArray();

			BASE64Encoder encoder = new BASE64Encoder();
			String objstr = encoder.encode(buff);

			return objstr;
		}
		catch (IOException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			try
			{
				if (null != out)
				{
					out.close();
				}
			}
			catch (IOException e)
			{
			}
			try
			{
				if (null != byteStream)
				{
					byteStream.close();
				}
			}
			catch (IOException e)
			{
			}
		}
	}

Total views.

© 2013 - 2018. All rights reserved.

Powered by Hydejack v6.6.1