获得图片的base64编码

记录一下~~~~

//获得图片的base64编码
	private String getBase64(String imgUrl) {
		URL url = null;
		InputStream in = null;
		HttpURLConnection httpUrl = null;
		byte[] data = null;
		
		try {
			url = new URL(imgUrl);
			httpUrl = (HttpURLConnection)url.openConnection();
			httpUrl.connect();
			in = httpUrl.getInputStream();
			data = new byte[in.available()];
			in.read(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}

	//获得图片的base64编码
//	private String getBase64(HttpServletRequest request, String src) {
//		InputStream in = null;
//		byte[] data = null;
//		String path = request.getSession().getServletContext().getRealPath("/") + src.substring(1);
//		try {
//			File file = new File(path);
//			if(!file.exists()) {
//				file.mkdirs();
//			}
//			in = new FileInputStream(file);
//			data = new byte[in.available()];
//			in.read(data);
//			in.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		BASE64Encoder encoder = new BASE64Encoder();
//		return encoder.encode(data);
//	}

 

你可能感兴趣的:(JavaWeb学习记录)