java提供的类库,并不是一切都能支持你现有的功能的,要敢于编写自己的类。
来源:互联网 发布:天津行知教育地址 编辑:程序博客网 时间:2024/06/06 00:19
又是老问题了,我以前一直不敢做,然而现在不得不作了。什么事情呢?
在java发送邮件时,需要添加附件,然而,我没有机会添加真是的文件,只有数据流,因为我是从数据库中得到的文件数据流,也来不及放到硬盘上,然而,javamail只支持FileSourceData的发送,怎么办?于是,我就反编译了其代码,并修改了自己需要的接口,然后实现了让其发送blob格式文件的BlobSourceData。带码入下,内容很简单,但是,却是我迈出的一大步。给我增加了不少自信。
/* * BlobDataSource.java * * Created on 2008.7.9, am11:48 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package mail;import java.io.InputStream;import java.io.OutputStream;import java.io.File;import java.io.FileDescriptor;import java.io.FileNotFoundException;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.sql.SQLException;import javax.activation.*;import oracle.sql.BLOB;import com.sun.activation.registries.MimeTypeFile;public class BlobDataSource implements DataSource { private BLOB _blob = null; private FileTypeMap typeMap = null; private String _fileName = null; public BlobDataSource(String fileName, BLOB blob) { _fileName = fileName; _blob = blob; } /** * This method will return an InputStream representing the * the data and will throw an IOException if it can * not do so. This method will return a new * instance of InputStream with each invocation. * * @return an InputStream */ public InputStream getInputStream() throws IOException { try{ return _blob.getBinaryStream(); }catch(SQLException expection){ return null; } } /** * This method will return an OutputStream representing the * the data and will throw an IOException if it can * not do so. This method will return a new instance of * OutputStream with each invocation. * * @return an OutputStream */ public OutputStream getOutputStream() throws IOException { try{ return _blob.getBinaryOutputStream(); }catch(SQLException expection){ return null; } } /** * This method returns the MIME type of the data in the form of a * string. This method uses the currently installed FileTypeMap. If * there is no FileTypeMap explictly set, the FileDataSource will * call the getDefaultFileTypeMap
method on * FileTypeMap to acquire a default FileTypeMap. Note: By * default, the FileTypeMap used will be a MimetypesFileTypeMap. * * @return the MIME Type * @see javax.activation.FileTypeMap#getDefaultFileTypeMap */ public String getContentType() { return "application/octet-stream"; } /** * Return the name of this object. The FileDataSource * will return the file name of the object. * * @return the name of the object. * @see javax.activation.DataSource */ public String getName() { return _fileName; } /** * Return the File object that corresponds to this FileDataSource. * @return the File object for the file represented by this object. */ public File getFile() { return null; } /** * Set the FileTypeMap to use with this FileDataSource * * @param map The FileTypeMap for this object. */ public void setFileTypeMap(FileTypeMap map) { typeMap = map; }}
其调用带码如下:
public boolean addFileAffix(String filename,BLOB blob) { Logger.println("add file affix:" + filename); try { BodyPart bp = new MimeBodyPart(); BlobDataSource streamds = new BlobDataSource(filename,blob); bp.setDataHandler(new DataHandler(streamds)); bp.setFileName(streamds.getName()); mp.addBodyPart(bp); return true; } catch (Exception e) { System.err.println("add file affix:" + filename + " error ecured" + e); return false; } }
- java提供的类库,并不是一切都能支持你现有的功能的,要敢于编写自己的类。
- 并不是所有的Fragment都能使用懒加载
- 敢于承担责任能彻底的改变你的命运
- Application.Exit()并不是在什么地方都能终止程序的运行
- 能改变一切的是意志
- 敢于面对 自己的 错误,能屈能伸
- 马蔚华:银行要敢于革自己的命
- Spring提供了编写DAO的支持类
- 你的美丽都让你带走,你的一切
- 你唯一能把握的是变成最好的自己
- 你唯一能把握的是,变成最好的自己
- 你唯一能把握的是变成最好的自己
- 今天是525:你真爱自己吗?-自我管理的最大瓶颈是你其实并不爱自己!
- 并不是所有的客户都看重价格的
- Android工具类,你能想到的都在这里
- 有时候。。。你以为的并不是你以为的。。。
- 并不是所有的程序员都适合做技术管理
- 并不是所有的程序员都适合做技术管理
- 小事将会把婚姻压碎
- IIS Web服务器已设置为允许匿名访问,但客户端仍然要求输入用户名和密码
- 一个缺乏责任感的人工作的成果自然就乏善可陈
- 如何设置 Web 服务器的权限之iis
- CreateWaitableTimer和SetWaitableTimer函数
- java提供的类库,并不是一切都能支持你现有的功能的,要敢于编写自己的类。
- yY2J’sblogczk68
- Tomcat下JSP、Servlet和JavaBean环境的配置
- 死锁
- 山村鬼事秘史(一)
- delphi ping
- 编程学什么语言?
- bpel中process提供的服务和调用的服务
- debian下面使用wine淘宝旺旺成功