国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

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

2024-09-05 00:17:42
字體:
來源:轉載
供稿:網友

1、jspsmart文件上傳(普通表單,帶有普通表單域、若干個文件選擇域)

頁面:

復制代碼 代碼如下:www.CuoXIn.com

<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>


處理類:
復制代碼 代碼如下:www.CuoXIn.com

/**
* 文件上傳
* @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、郵件發送:
復制代碼 代碼如下:www.CuoXIn.com

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丽江市| 孟州市| 永修县| 桃园县| 类乌齐县| 涟源市| 佛山市| 涿州市| 巴彦县| 东乌珠穆沁旗| 凌云县| 望谟县| 朝阳县| 林芝县| 峡江县| 平远县| 双桥区| 闽侯县| 光泽县| 龙州县| 漳州市| 嵊泗县| 延长县| 额尔古纳市| 宜兴市| 远安县| 丹东市| 广丰县| 宁陕县| 即墨市| 藁城市| 加查县| 开阳县| 武陟县| 科尔| 临汾市| 兴山县| 灵璧县| 青海省| 五华县| 梨树县|