请问一个struts2上传文件的问题 我写了一个struts2上传文件的例子,出现了一个很奇怪的问题,提交表单以后,页面直接跳转到result="input"的页面,action里面的断点也没有执行,而且浏览器上的url显示的是fileupload.action,页面却已经跳转到了input返回页面;如果file控件里不加文件,就会执行action里面的断点 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TSysUpfileAction.javapackage ms.platform.system.upfile;import java.io.File;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;@AnnotationForAction(menuCode="1622", pojoName = "TSysUpfile")public class TSysUpfileAction extends ActionSupport { private static final long serialVersionUID = 1L; private File upload; private String savePath; /**查询条件*/ private TSysUpfile schCondition; public TSysUpfile getschCondition() { return schCondition; } public void setschCondition(TSysUpfile TSysUpfile) { if(TSysUpfile == null) TSysUpfile = new TSysUpfile(); this.schCondition = TSysUpfile; } /**获取页面数据*/ private TSysUpfile TSysUpfile; public TSysUpfile getTSysUpfile() { return TSysUpfile; } public void setTSysUpfile(TSysUpfile tmgtupfile) { this.TSysUpfile = tmgtupfile; } /**翻页*/ private PageList pl; public PageList getpl() { return this.pl; } public void setpl(PageList pagelist) { this.pl = pagelist; } /**默认执行方法*/ @Override public String execute() throws Exception{ if(this.upload != null) return insertData(); return selectRecord(); } /**跳转到登记页面*/ @SuppressWarnings("unchecked") public String addRecord() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); List<Object> lstParameters = new ArrayList<Object>(); lstParameters.add(TSysUpfile); lstParameters.add(upload); setTSysUpfile(CrudImpl.addRecord( lstParameters )); return INPUT; } /**跳转到单查页面*/ @SuppressWarnings("unchecked") public String selectRecord() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); List<Object> lstParameters = new ArrayList<Object>(); lstParameters.add(TSysUpfile); setTSysUpfile(CrudImpl.selectRecord(lstParameters)); return INPUT; } /**插入数据*/ @SuppressWarnings("unchecked") public String insertData() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); String strPath = ServletActionContext.getServletContext().getContextPath(); List<Object> lstParameters = new ArrayList<Object>(); System.out.print("okok"); lstParameters.add(TSysUpfile); String filePath = strPath+savePath; TSysUpfile.setFilePath(filePath); //TSysUpfile.setFileType(fileType); CrudImpl.insertData( lstParameters ); return NONE; } /**修改数据*/ @SuppressWarnings("unchecked") public String updateData() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); List<Object> lstParameters = new ArrayList<Object>(); lstParameters.add(TSysUpfile); CrudImpl.updateData( lstParameters ); return NONE; } /**删除数据*/ @SuppressWarnings("unchecked") public String delete() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); List<Object> lstParameters = new ArrayList<Object>(); lstParameters.add(TSysUpfile); CrudImpl.delete(lstParameters); return NONE; } /**翻页数据查询*/ @SuppressWarnings("unchecked") public String selectPage() throws Exception { MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile"); if (pl==null) pl = new PageList(); List<Object> lstParameters = new ArrayList<Object>(); lstParameters.add(schCondition); lstParameters.add(pl); pl = CrudImpl.selectPage(lstParameters); return SUCCESS; } public File getUpload ( ) { return upload; } public void setUpload ( File upload ) { this.upload = upload; } public String getSavePath ( ) { return savePath; } public void setSavePath ( String savePath ) { this.savePath = savePath; } }fileupload.jsp<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %> <html><head><script language="javascript">function upfile(){ document.upfile.submit();}</script> </head><body> <s:form action="upfile" method="post" enctype="multipart/form-data"> <input type="button" value="de" onClick="upfile()"/> <s:submit value="axd" /> <table class="tblInfo" cellspacing="0" cellpadding="0"> <thead> <tr> <th colspan="4" >文件信息</th> </tr> </thead> <tbody> <tr> <td>请选择文件:</td> <td colspan="3"><s:file name="upload"/></td> </tr> </tbody> </table> </s:form></body></html> struts.xml<struts> <include file="struts-default.xml" /> <!-- 定义含“权限检查拦截器”的包,供其他模块引用 --> <package name="struts-default-with-auth-check" extends="struts-default"> <interceptors> <interceptor name="addbutton" class="ms.platform.base.interceptor.struts.AddButtonsInterceptor"/> <interceptor name="testfile" class="ms.platform.base.interceptor.struts.TestFile"/> <interceptor-stack name="mydefaultStacks"> <interceptor-ref name="addbutton"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 设置默认拦截器 --> <default-interceptor-ref name="mydefaultStacks"/> <!-- 设置全局跳转 --> <global-results> <result name="error">/public/dispatch/error.jsp</result> <result name="login">/product/login/login1.jsp</result> </global-results> <!-- 全局异常映射 --> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"/> </global-exception-mappings> </package> <package name="system" extends="struts-default-with-auth-check"> <!-- 文件上传列表 --> <action name="upfile" class="ms.platform.system.upfile.TSysUpfileAction" > <interceptor-ref name ="fileUpload"> <param name ="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg </param> </interceptor-ref> <interceptor-ref name ="defaultStack" /> <result name="input">/product/system/upfileInfo.jsp</result> <result name="none" type="redirect">upfile!selectPage.action</result> <result name="success">/product/system/upfileList.jsp</result> </action> </package> </struts> 配置问题,没有找到action可能 如果jsp页面里的file控件不赋值,就能跳入action,如果赋值,就不进入action,直接跳转到input指向的jsp,控制台和日志里没有错误提示 private File upload; action 里面生成 upload的get,set方法了吗? public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } if(this.upload != null) return insertData(); 这样判断有问题! 你直接提交到insertData(); 里试试,插入成功可以重定向啊 在if(this.upload != null) 增加断点,判断根本就进入不了 upload 是file对象 不能用this.upload 是不是null来判断的 或者不判断 或者直接用if(upload != null) return insertData(); 或者if(getUpload()!=null)来判断 你问题就出在这里 这个要怎么判断?我的upload取不到文件数据,每次提交,都是null struts.xml<!-- 文件上传列表 --> <action name="upfile" class="ms.platform.system.upfile.TSysUpfileAction" > <interceptor-ref name ="fileUpload"> <param name ="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg </param> </interceptor-ref> <result name="input">/product/system/upfileInfo.jsp</result> <result name="none" type="redirect">upfile!selectPage.action</result> <result name="success">/product/system/upfileList.jsp</result> </action>struts.propertiesstruts.multipart.saveDir=/tmpstruts.multipart.maxSize=100000000我选择的文件是jpg文件,大小为40K 在这个函数里下个断点public void setUpload(File upload) { this.upload = upload; }看一下 upload是不是空 你上传的文件是多大的,好像需要设置struts2的一个配置文件,那个配置文件中,有一个配置上传文件最大值,那个是struts2中校验,如果那个值比你上传的文件小,那么将不能通过,struts2的校验,故不会提交到action中,当初我们做项目时,我也遇到了同样的问题,后来把那个struts2中的值,设了10G,就解决了这个问题 用jquery判断用户名唯一,用ajax技术实现? 大家帮我解释下什么叫电信增值业务,并举几个例子! spring的action代理集成struts 多个action是时候出错 myeclipse中tomcat启动突然很慢,急,来者有分 Tomcat显示新JSP问题 tomcat5.5.17部署Web应用后,不能访问。 在线等,关于MAIL发邮件时,出现在的异常处理 在Session bean 中引用cmp中的函数 请问哪里可以找到ejb第二版的电子版下载? 求解惑,SSH框架整合提示加载struts.xml失败 多个下拉列表默认选值? 跪求高人来解决这个问题 在线等
package ms.platform.system.upfile;import java.io.File;import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;@AnnotationForAction(menuCode="1622", pojoName = "TSysUpfile")
public class TSysUpfileAction extends ActionSupport { private static final long serialVersionUID = 1L;
private File upload;
private String savePath;
/**查询条件*/
private TSysUpfile schCondition;
public TSysUpfile getschCondition() {
return schCondition;
}
public void setschCondition(TSysUpfile TSysUpfile) {
if(TSysUpfile == null)
TSysUpfile = new TSysUpfile();
this.schCondition = TSysUpfile;
}
/**获取页面数据*/
private TSysUpfile TSysUpfile;
public TSysUpfile getTSysUpfile() {
return TSysUpfile;
}
public void setTSysUpfile(TSysUpfile tmgtupfile) {
this.TSysUpfile = tmgtupfile;
}
/**翻页*/
private PageList pl;
public PageList getpl() {
return this.pl;
}
public void setpl(PageList pagelist) {
this.pl = pagelist;
}
/**默认执行方法*/
@Override
public String execute() throws Exception{
if(this.upload != null)
return insertData();
return selectRecord();
} /**跳转到登记页面*/
@SuppressWarnings("unchecked")
public String addRecord() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
List<Object> lstParameters = new ArrayList<Object>();
lstParameters.add(TSysUpfile);
lstParameters.add(upload);
setTSysUpfile(CrudImpl.addRecord( lstParameters ));
return INPUT;
} /**跳转到单查页面*/
@SuppressWarnings("unchecked")
public String selectRecord() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
List<Object> lstParameters = new ArrayList<Object>();
lstParameters.add(TSysUpfile);
setTSysUpfile(CrudImpl.selectRecord(lstParameters));
return INPUT;
} /**插入数据*/
@SuppressWarnings("unchecked")
public String insertData() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
String strPath = ServletActionContext.getServletContext().getContextPath();
List<Object> lstParameters = new ArrayList<Object>();
System.out.print("okok");
lstParameters.add(TSysUpfile);
String filePath = strPath+savePath;
TSysUpfile.setFilePath(filePath);
//TSysUpfile.setFileType(fileType);
CrudImpl.insertData( lstParameters ); return NONE;
} /**修改数据*/
@SuppressWarnings("unchecked")
public String updateData() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
List<Object> lstParameters = new ArrayList<Object>();
lstParameters.add(TSysUpfile);
CrudImpl.updateData( lstParameters ); return NONE;
} /**删除数据*/
@SuppressWarnings("unchecked")
public String delete() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
List<Object> lstParameters = new ArrayList<Object>();
lstParameters.add(TSysUpfile);
CrudImpl.delete(lstParameters);
return NONE;
} /**翻页数据查询*/
@SuppressWarnings("unchecked")
public String selectPage() throws Exception {
MultiParaCrudInterface<TSysUpfile> CrudImpl = (MultiParaCrudInterface<TSysUpfile>)ParameterPub.getCtx().getBean("upfile");
if (pl==null) pl = new PageList();
List<Object> lstParameters = new ArrayList<Object>();
lstParameters.add(schCondition);
lstParameters.add(pl);
pl = CrudImpl.selectPage(lstParameters);
return SUCCESS;
}
public File getUpload ( )
{
return upload;
}
public void setUpload ( File upload )
{
this.upload = upload;
}
public String getSavePath ( )
{
return savePath;
}
public void setSavePath ( String savePath )
{
this.savePath = savePath;
}
}fileupload.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<script language="javascript">
function upfile()
{
document.upfile.submit();
}
</script>
</head>
<body>
<s:form action="upfile" method="post" enctype="multipart/form-data">
<input type="button" value="de" onClick="upfile()"/>
<s:submit value="axd" />
<table class="tblInfo" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th colspan="4" >文件信息</th>
</tr>
</thead>
<tbody>
<tr>
<td>请选择文件:</td>
<td colspan="3"><s:file name="upload"/></td>
</tr>
</tbody>
</table>
</s:form>
</body>
</html> struts.xml<struts>
<include file="struts-default.xml" />
<!-- 定义含“权限检查拦截器”的包,供其他模块引用 -->
<package name="struts-default-with-auth-check" extends="struts-default">
<interceptors>
<interceptor name="addbutton" class="ms.platform.base.interceptor.struts.AddButtonsInterceptor"/>
<interceptor name="testfile" class="ms.platform.base.interceptor.struts.TestFile"/>
<interceptor-stack name="mydefaultStacks">
<interceptor-ref name="addbutton"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 设置默认拦截器 -->
<default-interceptor-ref name="mydefaultStacks"/>
<!-- 设置全局跳转 -->
<global-results>
<result name="error">/public/dispatch/error.jsp</result>
<result name="login">/product/login/login1.jsp</result>
</global-results>
<!-- 全局异常映射 -->
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"/>
</global-exception-mappings>
</package>
<package name="system" extends="struts-default-with-auth-check">
<!-- 文件上传列表 -->
<action name="upfile" class="ms.platform.system.upfile.TSysUpfileAction" >
<interceptor-ref name ="fileUpload">
<param name ="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg
</param>
</interceptor-ref>
<interceptor-ref name ="defaultStack" />
<result name="input">/product/system/upfileInfo.jsp</result>
<result name="none" type="redirect">upfile!selectPage.action</result>
<result name="success">/product/system/upfileList.jsp</result>
</action>
</package>
</struts>
action 里面生成 upload的get,set方法了吗? public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
}
return insertData();
这样判断有问题! 你直接提交到insertData(); 里试试,插入成功可以重定向啊
增加断点,判断根本就进入不了
或者不判断 或者直接用
if(upload != null)
return insertData();
或者if(getUpload()!=null)来判断
你问题就出在这里
<!-- 文件上传列表 -->
<action name="upfile" class="ms.platform.system.upfile.TSysUpfileAction" >
<interceptor-ref name ="fileUpload">
<param name ="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg
</param>
</interceptor-ref>
<result name="input">/product/system/upfileInfo.jsp</result>
<result name="none" type="redirect">upfile!selectPage.action</result>
<result name="success">/product/system/upfileList.jsp</result>
</action>struts.properties
struts.multipart.saveDir=/tmp
struts.multipart.maxSize=100000000我选择的文件是jpg文件,大小为40K
public void setUpload(File upload) {
this.upload = upload;
}
看一下 upload是不是空