关于邮件解析时utf-8部分乱码问题

在学习与邮件解析有关的项目时,使用javamail,由于网上的例子有各种bug,因此写下一点点自己碰到的问题:

由于part.getContent()现在得到的为InputStream类型,需要进行类型转换,于是在网上查找使用以下代码进行转换:

	        	String contentType = part.getContentType();
			ByteArrayOutputStream   baos = null;
	        	 if(part.isMimeType("text/plain")&&!conname){
	 	        	if(part.getContent() instanceof InputStream){
		        		StringBuffer   bodytext =   new   StringBuffer(); //1.bodytext用来获取读取到的信息
        				byte[]   b   =   new   byte[4096]; 
        				for   (int   n;   (n   =   in.read(b))   !=   -1;)   { 
                				bodytext.append(new   String(b,   0,   n)); //2.用byte数组接收并放入bodytext中

       					} 
		        	} 
		        	else{
		        		bodytext.append(part.getContent());
		        	} 
	 	        	String charset = "";
	 	        	if(contentType.contains("charset")){
	 	        		charset = contentType.substring
	 	        				(contentType.indexOf("charset")+"charset=".length()).replace("\"", "");
	 	        		if("".equals(charset)||charset.equals("gb2312"))
	 	        			charset = "gbk";
	 	        		if(baos!=null){
	 	        			bodytext = new StringBuffer()
 	        				.append(new String(bodytext.toString().getByte(),charset));//3.对读取到的boxytext按照正文字符集进行编码
} } }

 

charset为gbk的邮件可以有效的读取到但是charset为utf-8时会有部分读到的内容乱码,猜测可能是由于第2个注释中用byte数组接收并读取放入StringBuffer过程中new String出现部分字节读取发生转码等问题,因此改用ByteArrayOutputStream读取内容后进行转码,更正后代码如下:
<span style="white-space:pre">			</span>ByteArrayOutputStream   baos = null;
	        	 if(part.isMimeType("text/plain")&&!conname){
	 	        	if(part.getContent() instanceof InputStream){
		        		InputStream is = (InputStream) part.getContent();
		        		baos   =   new   ByteArrayOutputStream();
			        	int n;
			        	while((n =is.read()) != -1)   {
			        		baos.write(n);  
			        	}
		        	} 
		        	else{
		        		bodytext.append(part.getContent());
		        	} 
	 	        	String charset = "";
	 	        	if(contentType.contains("charset")){
	 	        		charset = contentType.substring
	 	        				(contentType.indexOf("charset")+"charset=".length()).replace("\"", "");
	 	        		if("".equals(charset)||charset.equals("gb2312"))
	 	        			charset = "gbk";
	 	        		if(baos!=null){
	 	        			bodytext = new StringBuffer()
 	        				.append(new String(baos.toByteArray(),charset));
	 	        		}
	 	        	}


 

测试读取内容后基本文字信息可完整读取,除了部分的分号与特殊字符无法读取外,不影响进一步的功能开发,So告一段落...

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值