//test_upload.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<html:form action="upload.do" enctype="multipart/form-data">
please select you file to upload:
<html:file property="file"/>
<html:submit property="submit">
</html:submit>
</html:form>
</head>
<body>
</body>
</html:html>
//struts-config.xml
<form-bean name="bookForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="file" type="org.apache.struts.upload.FormFile"/>
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
<action path="/test1" type="cn.java.TestCheckboxAction" name="registForm" scope="request">
</action>
<action path="/upload" type="cn.java.TestUploadAction" name="bookForm" scope="request" validate="false">
</action>
</action-mappings>
//action類
public class TestUploadAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response){
DynaActionForm dyform=(DynaActionForm)form;
FormFile theFile=(FormFile)dyform.get("file");
if(!theFile.getFileName().equals("")){
try{
InputStream stream=theFile.getInputStream();
String filePath="c://";
OutputStream bos=new FileOutputStream(filePath+theFile.getFileName());
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=stream.read(buffer,0, 8192))!=-1){
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
}catch(Exception e){
e.printStackTrace();
}
}
return null;
}
}