文件格式转换

 
/*
 * Base64.java
 *
 * Created on 2008-5-5, 11:11:32
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mop.pushmail.utils;

import javax.mail.internet.MimeUtility;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
//import java.io.RandomAccessFile;
/**
 *
 * @author muning
 */
public class Base64 {

    public Base64() {
    }
   
    public static BufferedReader decode(String b64string) throws Exception {
        return new BufferedReader(
                new InputStreamReader(
                MimeUtility.decode(
                new ByteArrayInputStream(b64string.getBytes()), "base64")));
    }
   
    public static void main(String args[]) throws Exception{
//        if (args.length != 2){
//            System.err.println("Usage: java Base64 encORdec yourStrings");
//            System.exit(0);
//        }
       
//        String str1 = args[0].trim();
//        String str1 = "dec";
//        String str2 = "好人";
//        String str3 = "";
//        if (str1.equalsIgnoreCase("enc")){
//            System.out.println(encodeAsString(str2));
//        }
//        else if(str1.equalsIgnoreCase("dec")){
//            System.out.println(decodeAsString(str3));
//        }
//        else{
//            System.err.println("Usage: java Base64 encORdec yourStrings");
//            System.exit(0);
//        }
//        String filename = "D://111.pdf";
//        Base64Encode(filename);
//        String filename1 = "D://111.txt";
//        Base64Decode(filename1);
       
//        System.out.println(System.getProperties());
        String test = "穆宁";
        byte[] bytes = test.getBytes("UTF-8");
        String result = new String(bytes, "UTF-8");
        System.out.println("Receive value: [" + result + "].");
    }
   
    public static String Base64Encode(String filename) {
        //int ret = -1;
        String content = null;
        FileInputStream fileforinput = null;
        try {
            fileforinput = new FileInputStream(filename);
            byte[] bytes = new byte[fileforinput.available()];  
            fileforinput.read(bytes);  
            content = new sun.misc.BASE64Encoder().encode(bytes);
            fileforinput.close();
            return content;
//            RandomAccessFile inOut = new RandomAccessFile("d://doc64.txt","rw");
//            inOut.write(content.getBytes());
//            inOut.close();
//            ret = 0;
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
       // return ret;
    }
   
    // 文件内容是base64编码的,还原成base64编码前的模样
    public static byte[] Base64Decode(String filename) {
        String original = null;
        FileInputStream fis = null;
        byte[] bytes = null;
        byte[] result = null;
        try {
            fis = new FileInputStream(filename);
            bytes = new byte[fis.available()];
            fis.read(bytes);
            original = new String(bytes);
//            String temp = new String(bytes,"utf-8");
//            original = new String(temp.getBytes("iso-8859-1"),"utf-8");
            result  = new sun.misc.BASE64Decoder().decodeBuffer(original.trim());  
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
   
    public static String decodeAsString(String b64string) throws Exception {
        if (b64string == null) {
            return b64string;
        }
       
        String returnString = decode(b64string).readLine();
        if (returnString == null) {
            return returnString;
        }
       
        return returnString.trim();
    }
   
    public static ByteArrayOutputStream encode(String plaintext,String charset) throws Exception{
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] in = plaintext.getBytes(charset);
        ByteArrayOutputStream inStream = new ByteArrayOutputStream();
        inStream.write(in, 0, in.length);
        // pad
        if ((in.length % 3 ) == 1){
            inStream.write(0);
            inStream.write(0);
        }
        else if((in.length % 3 ) == 2){
            inStream.write(0);
        }
       
        inStream.writeTo( MimeUtility.encode(out, "base64") );
        return out;
    }
   
     public static String encodeAsString(String plaintext) throws Exception {
        return encode(plaintext,"UTF-8").toString();
    }
   
    public static String encodeAsString(String plaintext,String charset) throws Exception {
        return encode(plaintext,charset).toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值