java提供的类库,并不是一切都能支持你现有的功能的,要敢于编写自己的类。

  又是老问题了,我以前一直不敢做,然而现在不得不作了。什么事情呢?

  在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;
            
        }
        
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值