首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

关于javamail 邮件附件内容(是内容不是附件名)乱码的有关问题

2012-01-20 
关于javamail 邮件附件内容(是内容不是附件名)乱码的问题本人现在正在做J2ME的手机邮件收发系统,现在已经

关于javamail 邮件附件内容(是内容不是附件名)乱码的问题
本人现在正在做J2ME的手机邮件收发系统,现在已经做到发送附件了,但是现在遇到了一个很难解决的问题,是发送的附件如果是纯文本的文件可以正常显示,但若是图片,word文档,等非纯文本附件就显示不了。我的代码如下:

手机的客户端的相关代码:

Java code
public byte[] uploadFile(String fileName){    // 上传附件        FileConnection fc = null ;        byte[] b = null ;        DataInputStream dis = null ;        String selectedFilePath ;                try {    // 此处要添加路径            fc = (FileConnection) Connector.open(HOME_PATH+currPath + fileName);            selectedFilePath = HOME_PATH + currPath + fileName ;            if(!fc.exists()){                return null;            }    //========================此处是读取文件的代码===========================//                b = new byte[(int)fc.fileSize()];            dis = fc.openDataInputStream();            dis.read(b);    // 将文件以字节的形式写入到字节数组里            dis.close();            if(fc != null){                fc.close();            }    //=================================================================//            PhoneMailMIDlet.EMAILMESSAGE.setHasFile(true);            PhoneMailMIDlet.EMAILMESSAGE.setFileName(fileName);            PhoneMailMIDlet.EMAILMESSAGE.setFilePath(selectedFilePath);            PhoneMailMIDlet.CTB_SCREEN.insert("(含附件)",0);        } catch (Exception e) {            e.printStackTrace();        }        return b ;    }



服务器端的相关代码:
Java code
DataInputStream dis=new DataInputStream(req.getInputStream());        from = dis.readUTF();        hostName = dis.readUTF();        password = dis.readUTF() ;        receiver = dis.readUTF();        subject = dis.readUTF();        content = dis.readUTF();        hasFile = dis.readBoolean();        if(hasFile){    //===========================读取客户端发过来附件的信息========================//            fileName = dis.readUTF();    //附件名            fileSize = dis.readInt();    // 附件的字节大小            fileByte = new byte[fileSize];    // 生成保存读取附件的字节数组,大小为附件大小            dis.read(fileByte);    // 获取文件的字节编码             System.out.println("附件的大小:"+fileByte.length);    //=========================================================================//        }        System.out.println("\n\n\n");        System.out.println("from:"+from);        System.out.println("Host Name:"+hostName);        System.out.println("receiver:"+receiver);        System.out.println("subject:"+subject);        System.out.println("content:"+content);        System.out.println("file name :"+fileName);        System.out.println("\n\n\n\n");                         ……                       ……public File byteToFile(byte[] b){        File attchFile = new File(fileName);        try {    //====================下是将读取的附件字节流转换成文件形式===========================//            FileOutputStream fos = new FileOutputStream(attchFile);//            DataOutputStream dos = new DataOutputStream(fos);            fos.write(b);            fos.close();            fos.close();    //==============================================================================//        } catch (Exception e) {            e.printStackTrace();            System.out.println("byte to file 出错....");        }        return attchFile;    }


[解决办法]
我也遇到了,解决了说一声哈,持续关注。。。。

热点排行