前端
文件上传var app = angular.module('app', ['ngFileUpload']);
app.controller('bmcuCtrl',
[ '$scope', 'Upload', function($scope, Upload) {
$scope.uploadFile = function() {
Upload.upload({
//服务端接收
url: 'api/customer/uploadBatchFile',
//上传的同时带的参数
data: {},
file: $scope.file
}).progress(function (evt) {
//进度条
var progressPercentage = parseInt(100.0 *
evt.loaded / evt.total);
console.log('progess:'+progressPercentage+
'%' + evt.config.file.name);
}).success(function (data, status, headers, config) {
//上传成功
console.log('file ' + config.file.name
+ 'uploaded. Response: ' + data);
}).error(function (data, status, headers, config) {
//上传失败
console.log('error status: ' + status);
});
}
} ]);
后端:
@Controller
@RequestMapping("/api/customer")
@ResponseBody
public class TestNgUpload{
@RequestMapping("/uploadBatchFile'")
public void uploadBatchFile'(HttpServletRequest request) throws Exception {
CommonsMultipartResolver multipartResolver =
new CommonsMultipartResolver(request.getSession().getServletContext());
if(multipartResolver.isMultipart(request)){
//再将request中的数据转化成multipart类型的数据
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
String name = (String) iter.next();
MultipartFile file = multiRequest.getFile(name);
String fileName = file.getOriginalFilename();
String format = fileName.substring(fileName.indexOf(".")+1);
InputStream inputStream = file.getInputStream();
Workbook wb = null;
if (format.equals("xls") || format.equals("msexcel")) {
wb = new HSSFWorkbook(inputStream);
}
if (format.equals("xlsx")) {
wb = new XSSFWorkbook(inputStream);
}
String[][] objArray = ExcelUtil.readExcel(wb, 1);
if(objArray.length>0){
BatchCustUnitVo batchCustUnitVo = new BatchCustUnitVo();
List accNbrList = new ArrayList();
List dataColList = new ArrayList();
for(int i=0;i
accNbrList.add(objArray[i][0].trim());
dataColList.add(objArray[i][1].trim());
}
batchCustUnitVo.setAccNbrList(accNbrList);
batchCustUnitVo.setDataColList(dataColList);
customerService.uploadBatchCustUnit(batchCustUnitVo);
}
}
}
}}