struts 批量上传

上一篇 / 下一篇  2006-12-30 13:44:33 / 个人分类:J2EE开发

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);
}
}

TAG: J2EE开发

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

数据统计

  • 访问量: 7640
  • 日志数: 15
  • 建立时间: 2006-12-07
  • 更新时间: 2007-01-11

RSS订阅

Open Toolbar