首页 技术 正文
技术 2022年11月21日
0 收藏 514 点赞 3,563 浏览 2920 个字

1、html部分

<form enctype="multipart/form-data">
  <label>请选择文件</label> <input id="file" class="file" type="file">
</form>

2、js部分

$("#file").fileinput({
uploadUrl: Config.Java_Manage_Path+'black/uploadBlack',
allowedFileExtensions: ['xls', 'xlsx'],
dropZoneEnabled: false,
browseLabel:"选择",
showRemove: false,
showUpload: false,
uploadLabel: "提交",
showCancel: false,
maxFileSize: 6000,
msgSizeTooLarge: '"{name}" ({size} KB) 不得超过 {maxSize} KB. 请重新选择文件',
fileActionSettings: {
showZoom: false,//不显示预览按钮
uploadTitle: "上传",
removeTitle: "删除"
},
progressUploadThreshold: "导入中,请稍后...",
msgInvalidFileExtension: '仅支持 "{extensions}" 类型的文件.'
});
//点击上传后隐藏关闭按钮
$('#file').on('filepreupload', function(event, data, previewId, index) {
// var form = data.form, files = data.files, extra = data.extra,
// response = data.response, reader = data.reader;
// console.log('File pre upload triggered');
$("#close").hide();
});
//上传完毕显示关闭按钮
$('#file').on('fileuploaded', function(event, data, previewId, index) {
/*var form = data.form, files = data.files, extra = data.extra,
response = data.response, reader = data.reader;
console.log('File uploaded triggered');*/
var response = data.response;
if(response.result == "ok"){
$.messager.popup("导入成功");
}else{
$.messager.popup("服务器可能出错了,请稍候再试");
}
$("#close").show();
$("#upload").modal("hide");
doSearch();
});

3、controller部分

 //创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(req.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(req)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) req;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
if(iter.hasNext()) {
// 取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
String fileName = file.getOriginalFilename(); if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")) {
this.response(req, res, false);
}
String newName = DateUtil.dateToStringT(new Date()) + fileName;
File targetFile = new File(upload_path, newName);
// 保存
// try {
// file.transferTo(targetFile);
// } catch (Exception e) {
// e.printStackTrace();
// this.response(req, res, false,ResultCode.getMessage(ResultCode.EXCEPTION));
// }
FileOutputStream os = null;
InputStream in = null;
// 保存
try {
os = new FileOutputStream(upload_path+"/"+newName);
//拿到上传文件的输入流
in = file.getInputStream();
//以写字节的方式写文件
int b = 0;
while((b=in.read()) != -1){
os.write(b);
} } catch (Exception e) {
e.printStackTrace();
this.response(req, res,false,
ResultCode.getMessage(ResultCode.EXCEPTION));
}finally{
os.flush();
os.close();
in.close();
}
// 读取excel
ExcelImportUtil excelUtil = new ExcelImportUtil();
excelUtil.setExcelPath(targetFile.getPath());
excelUtil.setStartReadPos(1);
List<Row> rowList = excelUtil.readExcel(); if (!(rowList != null && rowList.size() > 0)) {
this.response(req, res, false,
ResultCode.getMessage(ResultCode.FILE_READ_ERROR));
}
              //读取文件内容添加到数据库
Map<String, Object> map = blacklistservice.addBlackList( excelUtil, rowList);
if (map != null) {
this.response(req, res, JsonUtil.toJson(map));
} else {
this.response(req, res, false,
ResultCode.getMessage(ResultCode.EXCEPTION)); }
}
}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918