strtus配置<struts>
<package name="struts2" extends="struts-default" namespace="/web">
<action name="login" class="org.struts2.fileUpload.LoginAction">
<result name="success">/result.jsp</result>
</action>

</package>
</struts>
action
package org.struts2.fileUpload;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {
private User user;
private File file;
private String srcFileName;
private String srcContentType;
private int buffer_size = 16*1024;

public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
} public String getSrcFileName() {
return srcFileName;
}
public void setSrcFileName(String srcFileName) {
this.srcFileName = srcFileName;
} public String getSrcContentType() {
return srcContentType;
}
public void setSrcContentType(String srcContentType) {
this.srcContentType = srcContentType;
} private void copy(File src,File dest){
try{

InputStream is = new BufferedInputStream(new FileInputStream(src),buffer_size);
OutputStream os = new BufferedOutputStream(new FileOutputStream(dest),buffer_size);

byte[] buffer = new byte[buffer_size];
int len = 0;
int i = 0;

while(-1 != (len = is.read(buffer))){
os.write(buffer,0,len);
++i;
System.out.println("this is "+i+" time");
}
is.close();
os.close();
}catch(Exception ex){
ex.printStackTrace();
}

}



public String execute(){
String path = ServletActionContext.getRequest().getRealPath("/images");
File dest = new File(path);
copy(file,dest);

return SUCCESS;
}


}web配置 <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <filter>
   <filter-name>struts2_cleanup</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2_cleanup</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
login.jsp页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post" enctype="multipart/form-data" namespace="/web">
<s:textfield name="user.username" label="login name "/><br>
<s:password name="user.password" label="password"/><br>
<s:file name="file" label="browse"/>
<s:submit  value="submit"/>
</s:form>
</body>
</html>result.jsp页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
login name : ${requestScope.user.username }<br>
password : ${requestScope.user.password }<br>
pic name : ${requestScope.srcFileName }
</body>
</html>出错信息:
2011-7-12 20:23:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
java.io.FileNotFoundException: G:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\struts2FileUpload\images (拒绝访问。)

at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at org.struts2.fileUpload.LoginAction.copy(LoginAction.java:54)
at org.struts2.fileUpload.LoginAction.execute(LoginAction.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
2011-7-12 20:23:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Removing file file G:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\struts2FileUpload\upload__22517800_1311e4d7f94__8000_00000002.tmp

解决方案 »

  1.   

    login.jsp
    result.jsp
    上传文件的页面在哪里
      

  2.   

    login.jsp页面
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <s:form action="login" method="post" enctype="multipart/form-data" namespace="/web">
    <s:textfield name="user.username" label="login name "/><br>
    <s:password name="user.password" label="password"/><br>
    <s:file name="file" label="browse"/>
    <s:submit value="submit"/>
    </s:form>
    </body>
    </html>