Search in sources :

Example 31 with BASE64Encoder

use of sun.misc.BASE64Encoder in project portal by ixinportal.

the class SaveServiceTest method getPDFBinary.

/**
 *  将PDF转换成base64编码
 *  1.使用BufferedInputStream和FileInputStream从File指定的文件中读取内容;
 *  2.然后建立写入到ByteArrayOutputStream底层输出流对象的缓冲输出流BufferedOutputStream
 *  3.底层输出流转换成字节数组,然后由BASE64Encoder的对象对流进行编码
 */
static byte[] getPDFBinary(File file) {
    FileInputStream fin = null;
    BufferedInputStream bin = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bout = null;
    try {
        // 建立读取文件的文件输出流
        fin = new FileInputStream(file);
        // 在文件输出流上安装节点流(更大效率读取)
        bin = new BufferedInputStream(fin);
        // 创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量
        baos = new ByteArrayOutputStream();
        // 创建一个新的缓冲输出流,以将数据写入指定的底层输出流
        bout = new BufferedOutputStream(baos);
        byte[] buffer = new byte[1024];
        int len = bin.read(buffer);
        while (len != -1) {
            bout.write(buffer, 0, len);
            len = bin.read(buffer);
        }
        // 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
        bout.flush();
        byte[] bytes = baos.toByteArray();
        // return bytes;
        // sun公司的API
        String strpdf = new BASE64Encoder().encodeBuffer(bytes).trim();
        return strpdf.getBytes("utf-8");
    // return encoder.encodeBuffer(bytes).trim();
    // apache公司的API
    // return Base64.encodeBase64String(bytes);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        http: try {
            fin.close();
            bin.close();
            // 关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException
            // baos.close();
            bout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    http: return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BASE64Encoder(sun.misc.BASE64Encoder) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 32 with BASE64Encoder

use of sun.misc.BASE64Encoder in project portal by ixinportal.

the class ReceiptTest method test.

@Test
public void test() {
    // 正式系统地址
    String url = "https://www.fapiao.com:53087/fpt-dsqz/services/DZFPService";
    // String appId = "0d94a610c6e9a4260201b003bd78bdf8981af7906e4da43b96435bfaee4a089f"; //appid
    String appId = "2550ac3d539f7db6f859485af99e63de9882514145fbc2199d3f65f3bfece980";
    // String url = "https://dev.fapiao.com:19443/fpt-dsqz-spbm/services/DZFPService"; //测试地址
    // String appId = "6d29f136114544bcc73edcce960c430231183cc192c433e2b9ebcad56e8ceb08"; //appid
    // 执行命令后,会生成该testclient.truststore
    String ssl_store = getClass().getClassLoader().getResource("").getPath() + File.separator + "fapiao.truststore";
    // 证书的存取密码,即执行命令时填写的密码
    String ssl_pwd = "ixin21060921";
    System.setProperty("javax.net.ssl.trustStore", ssl_store);
    System.setProperty("javax.net.ssl.keyStorePassword", ssl_pwd);
    HostnameVerifier hv = new HostnameVerifier() {

        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    try {
        org.apache.axis.client.Service s = new org.apache.axis.client.Service();
        Call call = (Call) s.createCall();
        call.setTargetEndpointAddress(new URL(url));
        call.setOperation("doService");
        String content;
        String xml;
        String val;
        Map<String, String> temp;
        Long count = 0L;
        // 循环开票数量
        for (int i = 0; i < 0; i++) {
            count++;
            content = getContent(count);
            log.error("[input0]{}", content);
            xml = getCommonXml("DFXJ1001", new BASE64Encoder().encodeBuffer(content.getBytes("UTF-8")), appId);
            log.error("[input1]{}", xml);
            Object[] fn01 = { xml };
            val = (String) call.invoke(fn01);
            log.error("[output]{}", val);
            temp = parseXml(val);
            if (!temp.get("returnCode").equals("0000")) {
                log.error("ERRORLOG电子发票 {}", "开票推送失败,错误:" + temp.get("returnMessage"));
            }
            temp = parseXml(new String(new BASE64Decoder().decodeBuffer(temp.get("content")), "UTF-8"));
            log.error("pefurl=" + temp.get("PDF_URL"));
        }
    } catch (Exception e) {
        log.error("ERRORLOG电子发票 {}", "开票推送失败,错误:" + e.toString());
        e.printStackTrace();
    }
}
Also used : Call(org.apache.axis.client.Call) BASE64Encoder(sun.misc.BASE64Encoder) SSLSession(javax.net.ssl.SSLSession) AuthService(com.itrus.portal.evidence.controller.threeAppAPIService.AuthService) URL(java.net.URL) IOException(java.io.IOException) HostnameVerifier(javax.net.ssl.HostnameVerifier) JSONObject(com.alibaba.fastjson.JSONObject) BASE64Decoder(sun.misc.BASE64Decoder) Test(org.junit.Test)

Example 33 with BASE64Encoder

use of sun.misc.BASE64Encoder in project trainning by fernandotomasio.

the class LDAPNetworkUserDAO method encrypt.

private String encrypt(final String plaintext) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage());
    }
    byte[] raw = md.digest();
    String hash = (new BASE64Encoder()).encode(raw);
    return hash;
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 34 with BASE64Encoder

use of sun.misc.BASE64Encoder in project carbon-business-process by wso2.

the class BPMNInstanceService method encodeToString.

/**
 * Internally used method to encode a image to String
 *
 * @param image
 * @param type
 * @return encoded String
 */
private String encodeToString(BufferedImage image, String type) {
    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);
    } catch (IOException e) {
        log.error("Could not write image data", e);
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            log.error("Could not close the byte stream", e);
        }
    }
    return imageString;
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 35 with BASE64Encoder

use of sun.misc.BASE64Encoder in project summer by foxsugar.

the class MD5Util method getMD5.

public static String getMD5(String s) {
    MessageDigest md5 = null;
    String newstr = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        newstr = base64en.encode(md5.digest(s.getBytes("utf-8")));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return newstr;
}
Also used : BASE64Encoder(sun.misc.BASE64Encoder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

BASE64Encoder (sun.misc.BASE64Encoder)45 IOException (java.io.IOException)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 MessageDigest (java.security.MessageDigest)8 FileInputStream (java.io.FileInputStream)7 BufferedInputStream (java.io.BufferedInputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Map (java.util.Map)4 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 SSLSession (javax.net.ssl.SSLSession)4 Call (org.apache.axis.client.Call)4 BASE64Decoder (sun.misc.BASE64Decoder)4 File (java.io.File)3 URL (java.net.URL)3 Signature (java.security.Signature)3 X509Certificate (java.security.cert.X509Certificate)3 Date (java.util.Date)3