又是老問題了,我以前一直不敢做,然而現在不得不作了。什麼事情呢?
在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; } }