java zyUpload 实现多文件上传
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)); } } } }