主页 > 知识库 > jspsmart文件上传与邮件发送的实例

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

热门标签:服务器配置 阿里云 Mysql连接数设置 电子围栏 Linux服务器 银行业务 科大讯飞语音识别系统 团购网站

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");
ListFileItem> 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();
}
}

您可能感兴趣的文章:
  • 邮件发送简单例子-jsp文件
  • JSP发送邮件实例
  • node.js使用nodemailer发送邮件实例
  • Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
  • 纯javascript实现自动发送邮件
  • 基于Node.js实现nodemailer邮件发送

标签:萍乡 衢州 广元 大理 衡水 枣庄 江苏 蚌埠

巨人网络通讯声明:本文标题《jspsmart文件上传与邮件发送的实例》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266