摘录地址:
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);
}
}