java uploadify 上传组件使用方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
以上是关于java uploadify 上传组件使用方法的主要内容,如果未能解决你的问题,请参考以下文章