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();
}
分享到:
相关推荐
我的Struts2多文件上传实例 博文链接:https://zmx.iteye.com/blog/461026
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
struts2多文件上传显示进度 无插件
AjaxFileUpload Struts2 多文件上传
struts2 多文件上传下载 Java 文件上传下载 有界面,文件流实现上传下载(可多文件)
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
uploadify+struts2多文件上传实例,解决了在firefox下的session问题,相信能帮助到你的
文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...
ext结合struts2.0实现文件上传,代码详细,加载项目即可以看到效果。