Struts2 文件上传接到的值总是null

chuangzhijiayuan 2010-05-18 04:58:25
刚刚接触Struts2 实现文件上传时接到的值总是null 但多提交几次后能成功!求解!
...全文
398 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuangzhijiayuan 2010-05-19
  • 打赏
  • 举报
回复
问题解决,主要是配置了一下ActionContextCleanUp 的过滤器就可以了,ActionContextCleanUp 主要作用就是defer cleanup(也就是延长周期) 总之用struts标签最好配上,结贴
chuangzhijiayuan 2010-05-18
  • 打赏
  • 举报
回复
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 >

请帮忙看看,
redlotus_lyn 2010-05-18
  • 打赏
  • 举报
回复
贴贴代码...........

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧