!!! Declaration 1-3 is the content of the JSP page to be written in the text required util see Baidu Cloud http://pan.baidu.com/s/1kV0gqBt if expired please add QQ1940978083
1, first introduce CSS,JS file
<script type= "Text/javascript" src= "<%=path%>/js/uploadify/jquery.uploadify.min.js" ></script>
<link rel= "stylesheet" href= "<%=path%>/js/uploadify/uploadify.css " type= "Text/css" ></link>
2. Add HTML
<form class= "Form-horizontal" >
<div class= "Col-sm-6 form-group" >
<input type= "File" Name= "File_upload" id= "File_upload"/>
<p class= "Help-block" > Please upload files according to the template </p>
</div>
<div class= "Col-md-6" >
<button id= "btn" class= "btn btn-primary" >
<a style= "Color:white" href= "<%=path%>/document/black.xls" > Download template </a></button>
</div>
</form>
3. Call method
$ ("#upload #file_upload"). Uploadify ({
' Fileobjname ': ' File ',
' ButtonText ': ' Upload file ',
' swf ': ' <%=path%>/lib/js/uploadify/uploadify.swf ',
' Uploader ': ' <%=path%>/black/uploadblack.html?jsessionid=12 ',//upload path jsessionid is for Firefox upload to prevent session loss
' Filetypedesc ': ' Excel ',
' Auto ': true,
' FormData ': {': '},
' removecompleted ': false,
' filetypeexts ': ' *.xls;*.xlsx ',
' Onuploadstart ': function (file) {
$ ("#upload #file_upload"). Uploadify ("Settings", "FormData", {});
},
' Onuploadsuccess ': function (file, data, response) {
Results returned after a successful addition
}else{
Post-failure processing
}
});
4.
@RequestMapping ("Uploadblack")
public void Uploadblack (String jsessionid, @RequestParam (value = "File", Required = False) Multipartfile file,
HttpServletRequest req, HttpServletResponse res) throws IOException {
String path = Req.getsession (). Getservletcontext (). Getrealpath ("Upload");
String fileName = File.getoriginalfilename ();
if (!filename.endswith (". xls") &&!filename.endswith (". xlsx")) {
Writestringutf (Errorresult, RES);
Return
}
String newName = dateutil.datetostringt (New Date ()) + FileName;
File TargetFile = new file (path, newName);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
Save
try {
File.transferto (targetfile);
} catch (Exception e) {
E.printstacktrace ();
}
Read Excel
Excelimportutil excelutil = new Excelimportutil ();
Excelutil.setexcelpath (Targetfile.getpath ());
Excelutil.setstartreadpos (1);
List<row> rowlist = Excelutil.readexcel ();
if (rowlist! = null && rowlist.size () > 0) {
if (Jsessionid! = NULL &&! "". Equals (Jsessionid)) {
HttpSession session = Mysessioncontext.getinstance (). getsession (Jsessionid);
Domain domain = (domain) req.getsession (). getattribute ("Domainlogin");
map<string, object> map = blacklistservice.addblacklist (Domain.getdomainid (), Excelutil, rowList);
Targetfile.delete ();
if (map! = null) {
Writestringutf (Objecttojson (map), res);
} else {
Writestringutf (Errorresult, RES);
}
} else {
Targetfile.delete ();
Writestringutf (Errorresult, RES);
}
} else {
Targetfile.delete ();
Writestringutf (Errorresult, RES);
}
}
5. Realization Class
@Override
Public map<string, Object> addblacklist (Long Domainid,
Excelimportutil Excelutil, list<row> rowlist) {
/*list<blacklist> black = Blacklist.findall ("from Blacklist");
map<string, blacklist> usersmap = new hashmap<string, blacklist> ();
For (blacklist b:black) {
Usersmap.put (B.getnumber (), b);
}*/
Timestamp startTime = new Timestamp (new Date (). GetTime ());
String[] title = new String[] {"Number"};
map<string, object> result = new hashmap<string, object> ();
if (rowlist! = null && rowlist.size () > 0) {
for (Row row:rowlist) {
Blacklist black1 = new blacklist ();
StringBuffer info = new StringBuffer ();
for (int i = 0; i < title.length; i++) {
Info.append (Excelutil.getcellvalue (Row.getcell (i)) + ";");
System.out.println (Excelutil.getcellvalue (Row.getcell (i)));
if (Excelutil.getcellvalue (Row.getcell (i)) ==null&&excelutil.getcellvalue (Row.getcell (i)) = = "") {
Result.put ("Result", "false");
return result;
}
Black1.setnumber (Excelutil.getcellvalue (Row.getcell (i)));
Black1.setdomainid (Domainid);
Black1.setcreatetime (StartTime);
}
Blacklist.save (BLACK1);
}
Result.put ("Result", "OK");
/*result.put ("Sucnum", Orders.size ());
Result.put ("Errnum", notusersize); * *
return result;
}else{
Result.put ("Result", "false");
/*result.put ("Sucnum", 0);
Result.put ("Errnum", Rowlist.size ()); * *
return result;
}
}
How to use the Java uploadify Upload component