文件上传【一个文件或者多文件的上传】

本文详细介绍了如何使用commons-fileupload和commons-io库实现文件上传功能。包括配置表单、设置action类属性及处理上传流程等关键步骤。
文件上传:
第一步:加jar 包, commons-fileupload-1.2.1.jar, commons-io-1.3.2.jar
第二步:把form enctype属性设为"application/x-www-form-urlencoded"
<form action="${pageContext.request.contextPath }/xxx.action" method="post" enctype="multipart/form-data">
<input type="file" name="uploading"/>
</form>
第三步:action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
private File image;//得到上传的文件
private String imageFileName;//得到文件的名称:前面image就上传文件名称,,
后面是固定写法
private String imageContentType;//得到上传文件的类型
//上面的三个属性都是固定的写法,image是上传表单中name,其它的是固定写法
String realPath=ServletActionContext.getServletContext().getRealPath("/image");
System.out.println("真实路经:"+realPath);
System.out.println("文件名称:"+imageFileName);
if(image!=null){
File savefile=new File(new File(realPath),imageFileName);
if(!savefile.getParentFile().exists())savefile.getParentFile().mkdirs();
//image是源文件,后面是要把它放到那个目标路经,
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("msg", "文件上传成功!!");
}

多文件上传
如果要上传多个文件,
○1>form表单里的事name相同
○2>把action里的属性设为数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值