!!!声明 1-3 是jsp页面所写内容 文中需要的util 参见百度云 http://pan.baidu.com/s/1kV0gqBt 如已失效 请加QQ1940978083
1、首先引入css,js文件
2、 添加html
请按照模版上传文件
3、调用方法
$("#upload #file_upload").uploadify({
'fileObjName' : 'file',
'buttonText': '上传文件',
'swf' : '/lib/js/uploadify/uploadify.swf',
'uploader' : '/black/uploadBlack.html?jsessionid=12', //上传路径 jsessionid是为了火狐上传防止session丢失
'fileTypeDesc': 'Excel',
'auto' : true,
'formData' : {'':''},
'removeCompleted' : false,
'fileTypeExts': '*.xls;*.xlsx',
'onUploadStart' : function(file) {
$("#upload #file_upload").uploadify("settings","formData", {});
},
'onUploadSuccess' : function(file, data, response) {
//添加成功后返回的结果
}else{
//失败后的处理
}
});
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();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
// 读取excel
ExcelImportUtil excelUtil = new ExcelImportUtil();
excelUtil.setExcelPath(targetFile.getPath());
excelUtil.setStartReadPos(1);
List 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 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、实现类
@Override
public Map addBlackList(long domainid,
ExcelImportUtil excelUtil, List rowList) {
/*List black = blacklist.findAll("from Blacklist");
Map usersMap = new HashMap();
for (Blacklist b : black) {
usersMap.put(b.getNumber(), b);
}*/
Timestamp startTime = new Timestamp(new Date().getTime());
String[] title = new String[] {"number" };
Map result = new HashMap();
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;
}
}