java Blob文件转base64返回前端

public static String blobToBase64String(Blob blob) {
		String resBase64 = "";
		if(null != blob) {
			try {
				InputStream inputStream = blob.getBinaryStream();
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int n = 0;
				while (-1 != (n = inputStream.read(buffer))) {
					outputStream.write(buffer, 0, n);
				}
				resBase64 =new BASE64Encoder().encode(outputStream.toByteArray()) ;
				outputStream.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} 
				return "data:image/jpeg;base64,"+resBase64; 
			}else {
				return null;
			}
		}


 后台接收前端File 可通过 Hibernate.createBlob(new FileInputStream(file))               

oqcReport.setImg(Hibernate.createBlob(new FileInputStream(file)));

你可能感兴趣的:(java,java,开发语言)