摘錄地址:
http://blog.linuxmine.com/13259/viewspace-2576
1.html的form要設(shè)定:
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)) {
//不同的瀏覽器傳上的文件名可能有區(qū)別,有的是全路徑的
//在這里保存文件
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("出錯(cuò)了", ex);
}
}

歡迎大家訪問(wèn)我的個(gè)人網(wǎng)站
萌萌的IT人