日期:2014-05-20 浏览次数:20715 次
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 ; }
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; }