Struts2 文件上传接到的值总是null 刚刚接触Struts2 实现文件上传时接到的值总是null 但多提交几次后能成功!求解! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 action 类public class UploadAction extends ActionSupport { //文件标题 private String title; //要上传的文件 private File upload; //上传文件类型 private String uploadContentType; //上传文件名 private String uploadFileName; // 接受依赖注入的属性 private String savePath; //允许上传的类型 private String allowTypes; public String getAllowTypes() { return allowTypes; } public void setAllowTypes(String allowTypes) { this.allowTypes = allowTypes; } // 接受依赖注入的方法 public void setSavePath(String value) { this.savePath = value; } private String getSavePath() throws Exception { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setTitle(String title) { this.title = title; } public void setUpload(File upload) { this.upload = upload; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getTitle() { return (this.title); } public File getUpload() { return (this.upload); } public String getUploadContentType() { return (this.uploadContentType); } public String getUploadFileName() { return (this.uploadFileName); } public String filterType(String[] types){ String fileType=getUploadContentType(); for (String type:types){ if (type.equals(fileType)) return null; } return "input"; } @Override public String execute() throws Exception { String filterResult=filterType(getAllowTypes().split(",")); if (filterResult!=null){ ActionContext.getContext().put("typeError", "上传类型错误"); return filterResult; } // 以服务器的文件保存地址和原文件名建立上传文件输出流 FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()); FileInputStream fis = new FileInputStream(getUpload()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } return SUCCESS; }}struts.xml文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.custom.i18n.resources" value="globalMessages" /> <constant name="struts.i18n.encoding" value="GB2312" /> <package name="struts2" extends="struts-default"> <action name="uploadFilter" class="filter.UploadAction"> <param name="allowTypes"> image/bmp,image/png,image/gif,image/jpg,text/plain,application/msword </param> <param name="savePath">/</param> <result>/succ.jsp</result> <result name="input">/upload.jsp</result> </action> </package></struts>upload.jsp页面 <s:form action ="uploadFilter" method ="POST" enctype ="multipart/form-data" > <s:textfield name ="title" label =" 文件标题" /> <s:file name ="upload" label ="选择文件" /> <s:submit /> </s:form > 请帮忙看看, 问题解决,主要是配置了一下ActionContextCleanUp 的过滤器就可以了,ActionContextCleanUp 主要作用就是defer cleanup(也就是延长周期) 总之用struts标签最好配上,结贴 weblogic部署web应用报错 Struts2配置好的,访问就报错 java控制excel生成图表 ssl问题 证书不匹配 有关数学编辑器中mathML的问题.. 问个struts的问题啊 小弟请问一个问题,tomcat 5的admin的初始密码多少? cmp设置问题 求教一个初学者的问题,困扰我好久啦 oracle重启后,web应用及websphere需要重启吗 Jetty 如何 配置GZIP HTTP Status 500 怎么解决 初学者
public class UploadAction extends ActionSupport {
//文件标题
private String title;
//要上传的文件
private File upload;
//上传文件类型
private String uploadContentType;
//上传文件名
private String uploadFileName; // 接受依赖注入的属性
private String savePath;
//允许上传的类型
private String allowTypes; public String getAllowTypes() {
return allowTypes;
} public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
} // 接受依赖注入的方法
public void setSavePath(String value) {
this.savePath = value;
} private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
} public void setTitle(String title) {
this.title = title;
} public void setUpload(File upload) {
this.upload = upload;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getTitle() {
return (this.title);
} public File getUpload() {
return (this.upload);
} public String getUploadContentType() {
return (this.uploadContentType);
} public String getUploadFileName() {
return (this.uploadFileName);
} public String filterType(String[] types){
String fileType=getUploadContentType();
for (String type:types){
if (type.equals(fileType))
return null;
}
return "input";
}
@Override
public String execute() throws Exception {
String filterResult=filterType(getAllowTypes().split(","));
if (filterResult!=null){
ActionContext.getContext().put("typeError", "上传类型错误");
return filterResult;
}
// 以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
return SUCCESS;
}
}
struts.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.custom.i18n.resources"
value="globalMessages" />
<constant name="struts.i18n.encoding" value="GB2312" />
<package name="struts2" extends="struts-default">
<action name="uploadFilter" class="filter.UploadAction">
<param name="allowTypes">
image/bmp,image/png,image/gif,image/jpg,text/plain,application/msword
</param>
<param name="savePath">/</param>
<result>/succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>upload.jsp页面
<s:form action ="uploadFilter" method ="POST" enctype ="multipart/form-data" >
<s:textfield name ="title" label =" 文件标题" />
<s:file name ="upload" label ="选择文件" />
<s:submit />
</s:form > 请帮忙看看,