1, Jspsmart file upload (ordinary form, with a common form field, several file selection fields)
Page:
Copy Code code as follows:
<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>
<!--hidden file field 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>-->
<!--hidden file domain 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>
Processing class:
Copy Code code as follows:
/**
* File Upload
* @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 ());
Generate Dataid Directory
String NewPath = Estpath + "/" + dataid;
Createdir (NewPath);
Generate the Data directory
NewPath = Estpath + "/" + Dataid + "/data";
Createdir (NewPath);
Generate the Data directory
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 ();
Process File Upload
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 (); Delete temporary files for item
}
}else{
String Fvalue = item.getstring ();
if (Fvalue.indexof ("@")!=-1) {
Mailbox
email = fvalue;
System.err.println ("email:" + email);
}
}
}
}catch (Exception e) {
Flag = false;
System.err.println ("File upload failed!") "+ e);
}
}
Req.setattribute ("flag", flag);
Req.getrequestdispatcher ("/view/est.jsp"). Forward (req, resp);
}
2, the Mail sends:
Copy Code code as follows:
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 ();
Add attachment
public static void Addattachemnt (String fpath) {
Vfile.add (Fpath);
}
Send mail
public static void SendMail (String emailto,string msg) {
Vfile Attachment File Collection
try {
Properties Props = new properties ()//Get system environment
Authenticator auth = new Emailauthenticator (username, password);//Mail Service user authentication
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (props, auth);
Set session to communicate with mail server
MimeMessage message = new MimeMessage (session);
Set the address of the sender of the message
Message.setfrom (new internetaddress (username));
Set the address that the message receives
Message.addrecipient (Message.RecipientType.TO, New InternetAddress (Emailto));
Set Message subject
Message.setsubject (Mailsubject);
Structural multipart
Multipart MP = new Mimemultipart ();
Add body to multipart
MimeBodyPart content = new MimeBodyPart ();
Content.setcontent (msg, "text/html;charset=gb2312");
Mp.addbodypart (content);
Add an attachment to 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);
Set the period of mail delivery
Message.setsentdate (New Date ());
Message.savechanges ();
Send mail
Transport.send (message);
catch (Exception e) {
E.printstacktrace ();
}
}