java,php,asp.net,linux,javascript,mysql,mssql,oracle,编程

邮件转编码

转载请注明:http://www.pmjava.com/Article/ShowInfo.asp?ID=56617
*
* 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();
    }
}

posted on 2009-06-10 21:45 rrong_m 阅读(199) 评论(0)  编辑  收藏

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

随笔档案

文章分类

文章档案

java编程

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜