1. 程式人生 > >解析base64位,轉換為圖片

解析base64位,轉換為圖片

1.定義解析類

public class StringUtil {

	public static boolean isNotEmpty(String resurl) {
		// TODO Auto-generated method stub
		return false;
	}

	public static String getIdfaAndAppbid(String idfa, String appBID) {
		return idfa + "-" + appBID;
	}

	public static MultipartFile base64ToMultipart(String base64) {
		try {
			String[] baseStrs = base64.split(",");

			BASE64Decoder decoder = new BASE64Decoder();
			byte[] b = new byte[0];
			b = decoder.decodeBuffer(baseStrs[1]);

			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {
					b[i] += 256;
				}
			}

			return new BASE64DecodedMultipartFile(b, baseStrs[0]);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}

2.controllerz中定義MultipartFile file變數
3.通過file = StringUtil.base64ToMultipart(bo.getAvatarData());獲取,bo是自定義的實體類