欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jspsmart文件上传与邮件发送的实例

程序员文章站 2023-11-12 15:25:40
1、jspsmart文件上传(普通表单,带有普通表单域、若干个文件选择域) 页面:复制代码 代码如下:

1、jspsmart文件上传(普通表单,带有普通表单域、若干个文件选择域)

页面:

复制代码 代码如下:

<form class="form-horizontal" id=“estform” action="/tools/toolservlet?type=est" method="post" enctype="multipart/form-data">
<div class="control-group">
<label class="control-label" for="email">email</label>
<div class="controls">
<input type="text" id="email" name="email" placeholder="email" onblur="checkemail()">
<span class="help-inline"></span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="file">file</label>
<div class="controls">
<input type="file" name="file" id="file" onchange="getfileinfo()"/>
<span class="help-inline"></span>
</div>
</div>

<!-- 隐藏文件域 begin
<div class="control-group" id="hiddenfilediv" style="display: none;">
<label class="control-label" for="file">file</label>
<div class="controls">
<input type="file" name="file1" id="file1" />
<span class="help-inline"></span>
</div>
</div>-->
<!-- 隐藏文件域 end

<div class="control-group">
<label class="control-label" for="file">crossmatch</label>
<div class="controls">
<select name="crossmatch" id="crossmatch">
<option value="y">y</option>
<option value="n">n</option>
</select>
<span class="help-inline"></span>
</div>
</div>-->
<div class="control-group">
<div class="controls">
<!-- <a href="javascript:void(0);" id="upload" class="btn">submit</a>-->
<button type="submit" class="btn" id="upload">submit</button>
<button type="reset" class="btn" id="reset">reset</button>
</div>
</div>
</form>


处理类:
复制代码 代码如下:

/**
* 文件上传
* @param req
* @param resp
* @throws servletexception
* @throws ioexception
*/
protected void doupload(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
boolean flag = true;
string email = "";
string dataid = string.valueof(new date().gettime());
//生成dataid目录
string newpath = estpath + "/" + dataid;
createdir(newpath);
//生成data目录
newpath = estpath + "/" + dataid + "/data";
createdir(newpath);
//生成data目录
newpath = estpath + "/" + dataid + "/result";
createdir(newpath);
try{
diskfileitemfactory factory = new diskfileitemfactory();
servletfileupload upload = new servletfileupload(factory);
upload.setheaderencoding("utf-8");
list<fileitem> list = upload.parserequest(req);
for(fileitem item : list){
if(!(item.isformfield())){
system.err.println("item name:" + item.getname());
if((item!=null)&&(item.getname()!=null)&&(!(item.getname().equals("")))){
string uploadfilename = item.getname();
//处理文件上传
inputstream in = item.getinputstream();
int len = 0;
byte[] b = new byte[1024];
newpath = estpath + "/" + dataid + "/data/";
fileoutputstream out = new fileoutputstream(newpath + uploadfilename);
while((len=in.read(b))>0){
out.write(b, 0, len);
}
in.close();
out.close();
item.delete(); //删除item对应的临时文件
}
}else{
string fvalue = item.getstring();
if(fvalue.indexof("@")!=-1){
//邮箱
email = fvalue;
system.err.println("email:" + email);
}
}
}
}catch (exception e) {
flag = false;
system.err.println("文件上传失败!" + e);
}
}
req.setattribute("flag", flag);
req.getrequestdispatcher("/view/est.jsp").forward(req, resp);
}

2、邮件发送:
复制代码 代码如下:

public class emailattachservice {
private static string host = "smtp.163.com";
private static string username = "";
private static string password = "";
private static string mailsubject = "";
public static vector vfile = new vector();
//添加附件
public static void addattachemnt(string fpath){
vfile.add(fpath);
}
//发送邮件
public static void sendmail(string emailto,string msg) {
// vfile 附件文件集合
try {
properties props = new properties();// 获取系统环境
authenticator auth = new emailauthenticator(username, password);// 进行邮件服务用户认证
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");

session session = session.getdefaultinstance(props, auth);
// 设置session,和邮件服务器进行通讯
mimemessage message = new mimemessage(session);
// 设置邮件发送者的地址
message.setfrom(new internetaddress(username));
// 设置邮件接收的地址
message.addrecipient(message.recipienttype.to, new internetaddress(emailto));
// 设置邮件主题
message.setsubject(mailsubject);
// 构造multipart
multipart mp = new mimemultipart();
// 向multipart添加正文
mimebodypart content = new mimebodypart();
content.setcontent(msg, "text/html;charset=gb2312");
mp.addbodypart(content);
// 向multipart添加附件
enumeration efile = vfile.elements();
while(efile.hasmoreelements()){
mimebodypart fattach = new mimebodypart();
string fname = efile.nextelement().tostring();
filedatasource fds = new filedatasource(fname);
fattach.setdatahandler(new datahandler(fds));
fattach.setfilename(mimeutility.encodeword(fds.getname(), "gb2312",null));
mp.addbodypart(fattach);
}
vfile.removeallelements();
message.setcontent(mp);
// 设置邮件发送时期
message.setsentdate(new date());
message.savechanges();
//发送邮件
transport.send(message);
} catch (exception e) {
e.printstacktrace();
}
}