表单和文件同时上传。但action取不到
uploadContentType;
uploadFileName;
都为空。
///struts.xml///
<action name="adduser" class="adduseraction">
<result name="success">/WEB-INF/jsp/index.jsp
</result>
<result name="error">/WEB-INF/jsp/error.jsp
</result>
<param name="savePath">touxiangimages</param>
<param name="allowType">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param> </action>
///applicationContext.xml///
<bean id="adduseraction" class="com.rcjsb.struts.action.AddUserAction"
scope="prototype">
<property name="userservice" ref="userservice"></property>
</bean>
///AddUserAction.java///
public class AddUserAction extends ActionSupport {
.
.
.
        private File upload;
private String uploadContentType;
private String uploadFileName; public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
        public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;
}

解决方案 »

  1.   

    public String execute() throws Exception {

    String fileName=this.getSavePath()+"/"+this.getUploadFileName();

    System.out.println(this.getUploadFileName());///在这里都为空
    System.out.println(this.getUploadContentType());///在这里都为空
    System.out.println(fileName);


    FileOutputStream fos=new FileOutputStream(fileName);
    FileInputStream fis=new FileInputStream(this.getUpload());
    byte[] b=new byte[1024];
    int len=0;
    while((len=fis.read(b))>0)
    {
    fos.write(b,0,len);
    }
                    

    return SUCCESS;
    }
      

  2.   

    struts2的配置文件写的有问题!帮你改下<action name="adduser" class="adduseraction"> 
    <result name="success">/WEB-INF/jsp/index.jsp 
    </result> 
    <result name="error">/WEB-INF/jsp/error.jsp 
    </result> <interceptor-ref name="fileUpload"> 
    <param name="allowType">image/bmp,image/png,image/gif,image/jpeg,image/jpg </param> 
     <param name="maximumSize">2000000</param>       
     </interceptor-ref>
    <interceptor-ref name="defaultStack"/> 
    <param name="savePath">touxiangimages </param> 
    </action>   
    如果你的jsp表单提交写正确的话,这样就可以了。一起放在整个form里提交的话要这样写哈:<form action=" xxxxx " method="post" enctype="multipart/form-data" onsubmit="FFFFF">
     照片:  <input id="filePath" type="file" name="upload" value=""> <input class="btn" type="submit" onclick="FFFFF"  value="提交"/></form>这样就行了哈O(∩_∩)O~