jspsmart文件上傳與郵件發(fā)送的實例_JSP教程
推薦:ajax+json+Struts2實現(xiàn)list傳遞實例講解應付學習需要,需要通過ajax來獲取后臺的List集合里面的值,特做了一個實例并附上演示效果,希望本例對你有所幫助
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、郵件發(fā)送:
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);
}
//發(fā)送郵件
public static void sendMail(String emailTo,String msg) {
// vfile 附件文件集合
try {
Properties props = new Properties();// 獲取系統(tǒng)環(huán)境
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);
// 設置郵件發(fā)送者的地址
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);
// 設置郵件發(fā)送時期
message.setSentDate(new Date());
message.saveChanges();
//發(fā)送郵件
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
分享:整體刷新和局部刷新frameset窗口問題深入探討頁面分割,最常見的系統(tǒng)或網站的主界面,到這種這種分割頁面,大家首先想到是frameset,使用framset分割多種frame,這種方式簡單,接下來為大家介紹下局部刷新的問題
- jsp response.sendRedirect不跳轉的原因分析及解決
- JSP指令元素(page指令/include指令/taglib指令)復習整理
- JSP腳本元素和注釋復習總結示例
- JSP FusionCharts Free顯示圖表 具體實現(xiàn)
- 網頁模板:關于jsp頁面使用jstl的異常分析
- JSP頁面中文傳遞參數(shù)使用escape編碼
- 基于jsp:included的使用與jsp:param亂碼的解決方法
- Java Web項目中連接Access數(shù)據(jù)庫的配置方法
- JDBC連接Access數(shù)據(jù)庫的幾種方式介紹
- 網站圖片路徑的問題:絕對路徑/虛擬路徑
- (jsp/html)網頁上嵌入播放器(常用播放器代碼整理)
- jsp下顯示中文文件名及絕對路徑下的圖片解決方法
- 相關鏈接:
- 教程說明:
JSP教程-jspsmart文件上傳與郵件發(fā)送的實例
。