下面列出了 org.apache.commons.codec.binary.Base64 # encodeAsString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String encryptForYunpianV2(String info, String secret) throws UnsupportedEncodingException {
if (StringUtil.isNullOrEmpty(secret))
return info;
int[] key = getKeyByApikey(getApiSecret(secret));
byte[] bytes = encryptByTea(info, key);
Base64 base64 = new Base64();
return base64.encodeAsString(bytes);
}
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(td);
oos.close();
byte[] bytes = baos.toByteArray();
Base64 b = new Base64();
String str = b.encodeAsString(bytes);
j.writeStartObject();
j.writeStringField("digest", str);
j.writeEndObject();
}
}
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(td);
oos.close();
byte[] bytes = baos.toByteArray();
Base64 b = new Base64();
String str = b.encodeAsString(bytes);
j.writeStartObject();
j.writeStringField("digest", str);
j.writeEndObject();
}
}
public String toEncodedJson() {
Base64 base64 = new Base64(Integer.MAX_VALUE);
try {
return base64.encodeAsString(this.toJson().getBytes("UTF-8"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* This utility method serializes ElementPair into JSON + packs it into Base64-encoded string
*
* @return
*/
protected String toEncodedJson() {
ObjectMapper mapper = SequenceElement.mapper();
Base64 base64 = new Base64(Integer.MAX_VALUE);
try {
String json = mapper.writeValueAsString(this);
String output = base64.encodeAsString(json.getBytes("UTF-8"));
return output;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* It loads triples in a graph in the RDF store using HTTP PUT.
*
* @param triplesFilename the path of the file containing the triples to load.
* @param rdfSinkFolder the URI of the RDF SINK folder in the RDF Store.
* @param user the login required for authentication in the RDF store.
* @param password the password required for authentication in the RDF store.
* @return true if the triples have been loaded. False otherwise.
* @since 1.0
*/
public boolean loadDataIntoGraphByHTTP(final String triplesFilename, String rdfSinkFolder, final String user, final String password) {
boolean done = false;
//Get the triples file name
final File triplesFile = new File(triplesFilename);
final String triplesFilenameNoPath = triplesFile.getName();
//Append the file name to the rdfSinkFolder
if (rdfSinkFolder != null && !rdfSinkFolder.endsWith("/")) {
rdfSinkFolder = rdfSinkFolder + "/";
}
rdfSinkFolder = rdfSinkFolder + triplesFilenameNoPath;
//HTTP Authentication
final Base64 base64 = new Base64();
final byte[] authenticationArray = new String(user + ":" + password).getBytes();
final String encoding = base64.encodeAsString(authenticationArray);
try {
final URL putUrl = new URL(rdfSinkFolder);
final HttpURLConnection connection = (HttpURLConnection) putUrl.openConnection();
connection.setReadTimeout(2000);
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
//Write Authentication
connection.setRequestProperty ("Authorization", "Basic " + encoding);
//Write File Data
final OutputStream outStream = connection.getOutputStream();
final InputStream inStream = new FileInputStream(triplesFilename);
final byte buf[] = new byte[1024];
int len;
while ((len = inStream.read(buf)) > 0) {
outStream.write(buf, 0, len);
}
inStream.close();
final int responseCode=((HttpURLConnection)connection).getResponseCode();
if (responseCode>= 200 && responseCode<=202) {
done = true;
} else {
done = false;
}
((HttpURLConnection)connection).disconnect();
} catch (Exception exception) {
done = false;
LOGGER.error(MessageCatalog._00034_HTTP_PUT_FAILED, exception, rdfSinkFolder);
}
return done;
}
public static String base64UrlEncode(byte[] input) {
Base64 base64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, EMPTY_BYTE_ARRAY, true);
return base64.encodeAsString(input);
}
@Test
public void string_base64_encoding_apache () {
String randomPhrase = "Learn. Eat. Code.";
Base64 base64 = new Base64();
String encodedPhrase = base64.encodeAsString(randomPhrase.getBytes());
assertEquals("TGVhcm4uIEVhdC4gQ29kZS4=", encodedPhrase);
}