[摘录]struts 批量上传文件


摘录地址:http://blog.linuxmine.com/13259/viewspace-2576

1.html的form要设定:
enctype="multipart/form-data"


2.
<form-bean name="lazyForm" type="org.apache.struts.validator.LazyValidatorForm">
<form-property name="file" type="org.apache.struts.upload.FormFile[]"/>
</form-bean>


3.
String dirPath = getServlet().getServletContext().getRealPath("/")
+ "/upload";
Hashtable fileh = actionForm.getMultipartRequestHandler().
getFileElements();
for (Enumeration e = fileh.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
try {
FormFile formfile = (FormFile) fileh.get(key);
String filename = formfile.getFileName().trim(); //文件名
if (!"".equals(filename)) {
//不同的浏览器传上的文件名可能有区别,有的是全路径的
//在这里保存文件
InputStream ins = formfile.getInputStream();
OutputStream ōs = new FileOutputStream(dirPath +
File.separatorChar
+ filename);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();

}
} catch (Exception ex) {
logger.debug("出错了", ex);
}
}


欢迎大家访问我的个人网站 萌萌的IT人

posted on 2007-12-13 16:35 见酒就晕 阅读(1084) 评论(0)  编辑  收藏 所属分类: J2EE文章


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(3)

我参与的团队

随笔分类

随笔档案

文章分类

文章档案

收藏夹

BLOG

FRIENDS

LIFE

搜索

最新评论

阅读排行榜

评论排行榜