社区
Java EE
帖子详情
Struts2 文件上传接到的值总是null
chuangzhijiayuan
2010-05-18 04:58:25
刚刚接触Struts2 实现文件上传时接到的值总是null 但多提交几次后能成功!求解!
...全文
398
3
打赏
收藏
Struts2 文件上传接到的值总是null
刚刚接触Struts2 实现文件上传时接到的值总是null 但多提交几次后能成功!求解!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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
打赏
举报
回复
贴贴代码...........
[Java]用servlet实现
文件上传
功能
由于我老婆这边需要用到
文件上传
,然后这任务不知道怎么就到了我头上。她的框架是比较老的
struts2
(说实话我一度连
struts2
都拼写不对。。。) 文章目录背景介绍代码引入依赖tomcat依赖配置jar包依赖代码编写...
Struts2
内核分析
相信熟悉 Struts1 的程序员,对
Struts2
会迷惑,凡事是是而非。我也曾经遇到了这种情况。
Struts2
在设计的时候采用 webwork 的内核,尽量按照 Struts1 的编码习惯。 我不知道各位怎么学习 Struts1,当我阅...
Struts2
总结
1.全局的逻辑控制器为一个过滤器,负责过滤所有的请求。该过滤器引用的API类为 org.apache.
struts2
.disapatcher.FilterDispatcher 2.MVC:页面请求(jsp)——&...3.
struts2
的配置文件放在cl...
struts2
核心
相信熟悉 Struts1 的程序员,对
Struts2
会迷惑,凡事是是而非。我也曾经遇到了这种情况。
Struts2
在设计的时候采用 webwork 的内核,尽量按照 Struts1 的编码习惯。 我不知道各位怎么学习 Struts1,当我阅读了...
struts2
基础
http://struts.apache.org/ 一、
struts2
初体验 ppt2 ****************************************...第一个程序HelloWorld(导包,导配置文件struts.xml web.xml)从apps\
struts2
-blank工程下copy最少jar包:javassi...
Java EE
67,512
社区成员
225,880
社区内容
发帖
与我相关
我的任务
Java EE
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
复制链接
扫一扫
分享
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章