java uploadify 上传组件使用方法

!!!声明 1-3 是jsp页面所写内容   文中需要的util  参见百度云 http://pan.baidu.com/s/1kV0gqBt   如已失效 请加QQ1940978083

1、首先引入css,js文件

<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、 添加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">请按照模版上传文件</p>
</div>
<div class="col-md-6">
<button id="btn" class="btn btn-primary">
<a style="color:white" href="<%=path %>/document/black.xls">下载模板</a></button>
</div>
</form>

3、调用方法

$("#upload #file_upload").uploadify({
  'fileObjName' : 'file',
  'buttonText': '上传文件',
  'swf' : '<%=path%>/lib/js/uploadify/uploadify.swf',
  'uploader' : '<%=path%>/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<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、实现类

@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;
}
}

转载于:https://www.cnblogs.com/xdcr/p/5874692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值