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