java提供的類庫,並不是一切都能支援你現有的功能的,要敢於編寫自己的類。

來源:互聯網
上載者:User

  又是老問題了,我以前一直不敢做,然而現在不得不作了。什麼事情呢?

  在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;                    }            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.