flybar0015 2009-12-02 17:30
浏览 248
已采纳

struts2.1整合spring2.5后使用fileUpload的文件上传

struts2.1单独使用fileUpload没有问题,可以正常使用。

但是整合了spring2.5.6之后,就出现了一个让我匪夷所思的问题。

问题是这样:整合后,再上传文件,如果符合fileUpload的要求,那么上传没问题。

但是,如果文件上传的类型或者文件的大小不符合要求,那么本次上传失败。这也没问题。

问题是,在失败之后,我刷新页面,上传一个符合要求的文件,还是显示上传失败的信息。

而且文件的确上传失败了,后台也不走action了,感觉被fileUpload直接拦截了,

并且拦截器,获得的还是以前那个失败的信息。这个我就晕了。

有谁知道这个问题产生的原因是什么吗?请告知!谢谢!

以下是我使用的代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

index.jsp

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="upload" class="cn.sure.action.UploadAction">
</bean>

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">



/upload.jsp
/index.jsp
/upload

        <interceptor-ref name="fileUpload">
            <param name="maximumSize">204800</param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
    </action>
</package>

UploadAction.java

package cn.sure.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

private static final long serialVersionUID = 2865729575435431264L;

private File fj ;

private String savePath ;

private String fjContentType ;
private String fjFileName ;
private String test ;

@Override
public String execute() throws Exception {

    String path = ServletActionContext.getServletContext().getRealPath(savePath) ;

    InputStream in = new FileInputStream(fj) ;

    FileOutputStream out = new FileOutputStream(new File(path, fjFileName)) ;

    byte[] b = new byte[1024] ;

    int len = 0 ;

    while((len = in.read(b)) > 0){
        out.write(b, 0, len) ;          
    }

    in.close() ;
    out.close() ;

    return SUCCESS;
}

public File getFj() {
    return fj;
}
public String getFjContentType() {
    return fjContentType;
}
public String getFjFileName() {
    return fjFileName;
}
public String getTest() {
    return test;
}
public void setFj(File fj) {
    this.fj = fj;
}
public void setFjContentType(String fjContentType) {
    this.fjContentType = fjContentType;
}
public void setFjFileName(String fjFileName) {
    this.fjFileName = fjFileName;
}
public void setTest(String test) {
    this.test = test;
}

public String getSavePath() {
    return savePath;
}

public void setSavePath(String savePath) {
    this.savePath = savePath;
}

}

upload.jsp



[b]问题补充:[/b]
按照你说的我将


改为

结果。。。。。。,抛了个异常

Unable to instantiate Action, login, defined for 'login' in namespace '/'No Scope registered for scope 'propotype'
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:307)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

java.lang.IllegalStateException: No Scope registered for scope 'propotype'
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880)
com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:130)
com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

晕死,难道是不能这样用,还是我哪里用错了呢,唉,

原来以为我已经会整合ssh2了,结果今天才发现自己的无知!!!
[b]问题补充:[/b]

太阳啊,搞了一天了,终于知道问题出在哪里了

我太阳。。。。。。。。。。。。。。。。。。。。

单词写错了,太TM丢人了。。。。。。。。

prototype --- 我写的是 propotype :twisted:

另外我的springbean没有 singleton

只有scope,估计是版本的问题吧。

我用的是spring2.5.6

  • 写回答

3条回答 默认 最新

  • wocsok 2009-12-03 12:44
    关注

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题