最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
让javamail直接添加上传文件为附件的DataSource代码
时间:2008-01-12 编辑:简简单单 来源:一聚教程网
bodypart = new mimebodypart();
datasource datasource = new filedatasource("c:测试附件.doc");
bodypart.setdatahandler(new datahandler(datasource));
bodypart.setfilename(mimeutility.encodeword("测试附件.doc","gb2312", null));
multipart.addbodypart(bodypart);
由于javamail 的包里默认的对javax.activation.datasource只有两个实现:
分别是:filedatasource和urldatasource。
因此在webapp里为了不把上传的文件再保存为本地文件,然后再使用filedatasource,
我结合apache的commons fileupload组件,写了一个实现了datasource的uploadfiledatasource。
其实代码非常简单,具体代码如下:
package com.lizongbo.util;
import java.io.*;
import javax.activation.*;
import org.apache.commons.fileupload.fileitem;
/**
*
title: uploadfile datasource for javamail
*
description:
*
copyright: copyright (c) 2005
*
company: zongboli
* @author lizongbo
* @version 1.0
*/
public class uploadfiledatasource implements datasource {
private fileitem uploadfileitem = null;
public uploadfiledatasource() {
}
public uploadfiledatasource(fileitem uploadfile) {
this.uploadfileitem = uploadfile;
}
public string getcontenttype() {
return uploadfileitem.getcontenttype();
}
public inputstream getinputstream() throws ioexception {
return uploadfileitem.getinputstream();
}
public string getname() {
return uploadfileitem.getname();
}
public outputstream getoutputstream() throws ioexception {
return null;
}
public static void main(string[] args) {
}
}
附在struts里的使用例子:
if (diskfileupload.ismultipartcontent(servletrequest)) {
-
上一个: 在jsp中发送email !
相关文章
- java web上传文件和下载文件代码片段分享 03-04
- ckeditor解决上传文件路径方法 11-01
- java中使用Struts 2框架上传文件代码 10-31
- 让你同时上传1000个文件 03-12
- 改变上传文件框的样式 01-16
- 通过Web Services上传和下载文件 01-12