jquery java 文件上传_SpringMVC+jquery实现文件上传教程

上传文件有很多种方法,这里主要讲依赖commons-io.jar和commons-fileupload(版本没有太大要求,能实现功能即可)实现,楼主用的是commons-fileupload-1.3.1.jar和commons-io-2.4.jar。

主pom.xml配置

[html] view plain copy

commons-io

commons-io

${commons.io.version}

commons-fileupload

commons-fileupload

${commons.fileupload.version}

除了引入上述两种jar包,还可以在spring配置文件中添加id为multipartresolver的bean,用于控制文件上传的大小,

[html] view plain copy

准备工作做好后,开始编写前端代码。前端用form表单,enctype一定要为"multipart/form-data",否则无法实现文件的上传。

[html] view plain copy

action="${ctx}/enduser/upload"

enctype="multipart/form-data" method="post">

aria-hidden="true">×

导入excel

文件选择:

文件上传:

form提交时通过jquery校验文件上传的格式,楼主这里实现上传excel文件,所以校验excel格式

[html] view plain copy

$("#file_form").submit(

function() {

//首先验证文件格式

var filename = $('#file_input').val();

if (filename === '') {

alert('请选择文件');

return false;

}

var filetype = (filename.substring(filename

.lastindexof(".") + 1, filename.length))

.tolowercase();

if (filetype !== 'xls' && filetype !== 'xlsx') {

alert('文件格式不正确,excel文件!');

return false;

}

}

});

仅校验是不够的,还需要用ajaxsubmit进行假提交,即不真正提交文件到服务器,而是只在后台获取上传的文件

[html] view plain copy

$("#file_form").submit(

$("#file_form").ajaxsubmit({

datatype : "json",

clearform: true,

success : function(data, textstatus) {

if (data.returncode == 1) {

console.log('上传文件成功');

alert("上传文件成功");

} else {

console.log('文件格式错误');

alert("文件格式错误");

}

return false;

}

});

return false;

});

后台通过form的action转发的url获取上传的文件,方法名和url同名,文件则是以multipartfile类型传递的

[java] view plain copy

/**

* /enduser/upload

* 导入excel

* @param request

* @throws exception

*/

@requestmapping(value="/upload",method= {requestmethod.post})

@responsebody

public string upload(@requestparam(value = "file") multipartfile file,httpservletrequest request) throws exception  {

//判空

if(file.isempty()) {

return null;

}

logger.debug("原始文件名称:"+file.getname());

logger.debug("==========开始转换excel文件==========");

//multipartfile转换为file

file f = multiparttofile(file);

try {

enduserprovider.importexcel(f);

catch (exception e) {

logger.error("==========解析excel文件失败==========",e);

return "redirect:/enduser/list";

}

值得注意的是,文件通过multipartfile类型传到后台时,若文件为空,multipartfile不为空,而是用multipartfile.isempty()判空。

文件由multipartfile转为file后,由file得到文件的输入流is,通过workbookfactory创建workbook对象

[java] view plain copy

fileinputstream is = new fileinputstream(file);

workbook = workbookfactory.create(is);

is.close();

最后就是通过workbook对象得到sheet对象,然后得到excel的行和列,分别遍历行列进行文件的处理啦

[java] view plain copy

for(int i=0;i

//获取excel文件的第一个sheetsheet sheet = workbook.getsheetat(i);

//获取文件的行

int totalrows = sheet.getphysicalnumberofrows();

//循环excel行数

for (int r = 1; r < totalrows; r++) {

row row = sheet.getrow(r);

if (row == null){

continue;

}

//遍历列

for (int c = 0; c < totalcells; c++) {

cell cell = row.getcell(c);

//文件处理

}

}

}

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值