private static String encodeObject(Object actualObject) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(actualObject); objectOutputStream.flush(); objectOutputStream.close(); String string = new Base64(76, LINE_SEPARATOR, false).encodeAsString(byteArrayOutputStream.toByteArray()); String trimmedString = Verify.removeFinalNewline(string); return Verify.addFinalNewline(trimmedString); } catch (IOException e) { throw new AssertionError(e); } }
if (val instanceof byte[]) { Base64 base64 = new Base64(); res = base64.encodeAsString((byte[]) val); } else if (val instanceof Blob) { byte[] b = blobToBytes((Blob) val); Base64 base64 = new Base64(); res = base64.encodeAsString(b); } else if (val instanceof String) { res = base64.encodeAsString(b);
/** * Base64 encode. * * @param input * the input * @return the base64 encoded input */ public static final String encode(byte[] input) { Base64 base64 = new Base64(); return base64.encodeAsString(input); }
@Override public void serialize(byte[] value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { String text; if (value == null) { text = null; } else if (value.length == 0) { text = ""; } else { text = BASE64.encodeAsString(value); } jgen.writeString(text); }
/** * Base64 encode. * * @param input * the input * @return the base64 encoded input */ public static final String encode(byte[] input) { Base64 base64 = new Base64(); return base64.encodeAsString(input); }
@Override public String encrypt(final String content, final Object salt) { try { final Cipher cipher = buildCipher(salt, Cipher.ENCRYPT_MODE); final byte[] plainTextBytes = content.getBytes(UTF8); final byte[] cipherText = cipher.doFinal(plainTextBytes); return BASE64.encodeAsString(cipherText); } catch (final Exception e) { throw new RuntimeException("Failed to encrypt", e); } }
/** * Create auth string from username and password. * * @return basic authentication string */ public String getAuthString() { String credentials = this.username + ":" + this.password; try { String encoded = new Base64().encodeAsString(credentials.getBytes("ascii")); return "Basic " + encoded; } catch (final UnsupportedEncodingException e) { throw new InvalidRequestException("It must be possible to encode credentials as ascii", credentials, e); } }
/** * Create auth string from username and password. * * @return basic authentication string */ public String getAuthString() { String credentials = this.username + ":" + this.password; try { String encoded = new Base64().encodeAsString(credentials.getBytes("ascii")); return "Basic " + encoded; } catch (final UnsupportedEncodingException e) { throw new InvalidRequestException("It must be possible to encode credentials as ascii", credentials, e); } }
public static String generateBase64Key() { byte[] hexBytes = Hex.decode(UUID.randomUUID().toString().replace("-", "")); return new Base64().encodeAsString(hexBytes); }
/** * Serializes the given {@link Serializable} object and then executes a * Base 64 encoding on it, returning the encoded value as a String. * * @param object the object to serialize, cannot be null * @return a base 64-encoded representation of the serialized object * * @throws IllegalArgumentException if the given object is null * @throws SerializationException if the serialization fails */ public static String serializeToBase64(Serializable object) { if (object == null) { throw new IllegalArgumentException("Cannot serialize a null object"); } byte[] serializedBytes = org.apache.commons.lang.SerializationUtils.serialize(object); return new Base64().encodeAsString(serializedBytes); }
private final HttpPost createHttpPost(final Map<String, Serializable> options, final String apiInterfaceName, final String methodName, final List<String> classNameParameters, final Object[] parametersValues) throws IOException { final HttpEntity httpEntity = buildEntity(options, classNameParameters, parametersValues); final StringBuilder sBuilder = new StringBuilder(serverUrl); sBuilder.append(SLASH).append(applicationName).append(SERVER_API).append(apiInterfaceName).append(SLASH).append(methodName); final HttpPost httpPost = new HttpPost(sBuilder.toString()); httpPost.setEntity(httpEntity); // Basic authentication if (basicAuthenticationActive) { final StringBuilder credentials = new StringBuilder(); credentials.append(basicAuthenticationUserName).append(":").append(basicAuthenticationPassword); final Base64 encoder = new Base64(); final String encodedCredentials = encoder.encodeAsString(credentials.toString().getBytes("UTF-8")); httpPost.setHeader("Authorization", "Basic " + encodedCredentials); } return httpPost; }
private final HttpPost createHttpPost(final Map<String, Serializable> options, final String apiInterfaceName, final String methodName, final List<String> classNameParameters, final Object[] parametersValues) throws IOException { final HttpEntity httpEntity = buildEntity(options, classNameParameters, parametersValues); final StringBuilder sBuilder = new StringBuilder(serverUrl); sBuilder.append(SLASH).append(applicationName).append(SERVER_API).append(apiInterfaceName).append(SLASH).append(methodName); final HttpPost httpPost = new HttpPost(sBuilder.toString()); httpPost.setEntity(httpEntity); // Basic authentication if (basicAuthenticationActive) { final StringBuilder credentials = new StringBuilder(); credentials.append(basicAuthenticationUserName).append(":").append(basicAuthenticationPassword); final Base64 encoder = new Base64(); final String encodedCredentials = encoder.encodeAsString(credentials.toString().getBytes("UTF-8")); httpPost.setHeader("Authorization", "Basic " + encodedCredentials); } return httpPost; }
private static String encodeObject(Object actualObject) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(actualObject); objectOutputStream.flush(); objectOutputStream.close(); String string = new Base64(76, LINE_SEPARATOR, false).encodeAsString(byteArrayOutputStream.toByteArray()); String trimmedString = Verify.removeFinalNewline(string); return Verify.addFinalNewline(trimmedString); } catch (IOException e) { throw new AssertionError(e); } }
private static String encodeObject(Object actualObject) { try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(actualObject); objectOutputStream.flush(); objectOutputStream.close(); String string = new Base64(76, LINE_SEPARATOR, false).encodeAsString(byteArrayOutputStream.toByteArray()); String trimmedString = Verify.removeFinalNewline(string); return Verify.addFinalNewline(trimmedString); } catch (IOException e) { throw new AssertionError(e); } }
@RequestMapping("/download") public void download(HttpServletRequest request, HttpServletResponse response) throws Exception { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/msword"); // 下载文件的默认名称 response.setHeader("Content-Disposition", "attachment;filename=xx.doc"); freeMarkerConfigurer.getConfiguration().setClassForTemplateLoading(getClass(), "/"); Template template = freeMarkerConfigurer.getConfiguration().getTemplate("userList.ftl"); String webapp = request.getServletContext().getRealPath("/"); List<User> list = userRepository.findAll(); for (int i = 0; i < list.size(); i++) { User user = list.get(i); File file = new File(webapp, user.getHeadPortrait()); FileInputStream fis = new FileInputStream(file); byte[] imgData = new byte[fis.available()]; fis.read(imgData); fis.close(); String headPortrait = b64Encoder.encodeAsString(imgData); user.setHeadPortrait(headPortrait); } Map<String, Object> root = new HashMap<String, Object>(); root.put("list", list); template.process(root, new OutputStreamWriter(response.getOutputStream())); } }
Base64 b = new Base64(); String authString = rpcuser + ":" + rpcpass; String encoding = b.encodeAsString(authString.getBytes()); connection.setRequestProperty("Authorization", "Basic " + encoding); connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
final String encoding = base64.encodeAsString(authenticationArray);
Base64 b = new Base64(); String authString = rpcuser + ":" + rpcpass; String encoding = b.encodeAsString(authString.getBytes()); connection.setRequestProperty("Authorization", "Basic " + encoding); connection.setRequestProperty("Content-Length", Integer.toString(requestBody.getBytes().length));
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 authorized(HttpUriRequest request, String service, String module, String function, Long timestamp) { String sign = new Base64().encodeAsString(SignUtil.sign(getClientSecret(), module, function, timestamp)); request.setHeader("X-GS2-CLIENT-ID", getClientId()); request.setHeader("X-GS2-REQUEST-TIMESTAMP", String.valueOf(timestamp)); request.setHeader("X-GS2-REQUEST-SIGN", sign); } }