页面中引入引入:fileinput.js

js初始化:

<div class="modal-body">
<input type="file" id="customerexcel" name="file" accept=".xls,.xls" multiple/>
</div>

后台js进行初始化;


$("#customerexcel").fileinput({
language : 'zh', // 设置语言
uploadUrl : getProjectURL() + 'customer/drcustomer', // 上传的地址
allowedFileExtensions : [ 'xls' ],// 接收的文件后缀
showUpload : true, // 是否显示上传按钮
showCaption : false,
maxFileCount : 1
// 是否显示标题
}).on('fileuploaded', function(event, data) {
alert(data.response.msg);
})

最终的实现结果:

bootstrap 文件上传fileinput_初始化

因为我这个是导入的excel,后台:

  @RequestMapping("/drcustomer")
@ResponseBody
public JSONObject drCustomer(HttpServletResponse response, HttpServletRequest request) {
try {
int count = 0;
if (ServletFileUpload.isMultipartContent(request)) {
MultipartHttpServletRequest fileUpload = (MultipartHttpServletRequest) request;
MultipartFile file = fileUpload.getFile("file");
Workbook workbook = Workbook.getWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
for (int i = 1; i < rows; i++) {
Cell[] cells = sheet.getRow(i);
Customer customer = new Customer();
customer.setCode(cells[0].getContents());

// 判断是否存在相同的代码
if (customerService.isExistSameCode(customer.getCode())) {
count++;
continue;
}
customer.setCusname(cells[1].getContents());
customer.setComtaxno(cells[2].getContents());
customer.setAddresstel(cells[3].getContents());
customer.setSj(cells[4].getContents());
customer.setEmail(cells[5].getContents());
customer.setAccount(cells[6].getContents());
customer.setAddtime(new Date());
customerService.save(customer);
}
String msg="";
if(count>0) {
msg="客户导入成功:有" + count + "在相同的客户代码,没有导入";
}else {
msg="客户导入成功";
}
return BuildJsonOfObject.getJsonString("1",msg);
} else {
return BuildJsonOfObject.getErrorJSONString("0", "文件上传失败");
}
}catch(BiffException e) {
throw new RuntimeException("内容解析失败,请将当前excel另存为Microsoft Excel 97-2003 文件的*.xls");
} catch (Exception e) {
e.printStackTrace();
return BuildJsonOfObject.getErrorJSONString("0", e.getMessage());
}

}

希望对有所帮助