程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jspsmart文件上傳與郵件發送的實例

jspsmart文件上傳與郵件發送的實例

編輯:關於JSP

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved