struts2多文件上传

jsp页面

<form action="${pageContext.request.contextPath}/user/fileUpload.action"
//文件上传这个不能没有
enctype="multipart/form-data"
method="post">
文件1:<input type="file" name="upload"><br/>
文件2:<input type="file" name="upload"><br/>
文件3:<input type="file" name="upload"><br/>
<input type="submit" value="上传" />
</form>


struts.xml

//设置上传文件大小,默认是2M 不设置只能上传2M大了就会报错
<constant name="struts.multipart.maxSize" value="10485760"/>


action

//要有get()、set()方法
private List<File> upload;
private List<String> uploadFileName;
private List<String> uploadContentType;

String root=Common.PICTRUE_ROOT_DIR;
File dir=FileUtil.createDir(root);
for(int i=0;i<upload.size();i++){
System.out.println(i);
InputStream is=new FileInputStream(upload.get(i));
String fileName=this.getUploadFileName().get(i);
Long time=System.currentTimeMillis();
String saveFileName=time.toString()+fileName.substring(fileName.lastIndexOf("."));
File uploadFile=new File(dir,saveFileName);
String path=FileUtil.getSubPath(Common.PICTRUE_ROOT_DIR, uploadFile);
String filepath =path.substring(0,path.lastIndexOf("\\"))+"\\" + fileName;
map.put(i,filepath);
OutputStream os=new FileOutputStream(uploadFile);
byte[] buffer=new byte[1024*1024];
int length;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值