angularjs java 文件上传_ng-file-upload文件上传

本文介绍了一个使用Angular实现的前端文件上传示例,通过Angular的ngFileUpload模块完成文件选择与上传操作,并展示了如何利用进度条显示上传进度。同时,后端采用Java接收上传的文件并解析Excel数据。
摘要由CSDN通过智能技术生成

前端

文件上传

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);

}

}

}

}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值