使用struts2上传图片

时间:2012-11-12 11:39:00

标签: struts2

我想使用函数copyFile()使用struts2上传图像 但是当我使用不受支持的ServletRequestAware时,抛出了异常。请帮我解决这个问题。 这是我的代码

的index.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
    <body>
        <s:form action="uploadAction.action" method="post" enctype="multipart/form-data">>
            <s:textfield label="caption" name="caption"/>
            <input type="file" name="userImage"/>
            <s:submit name="submit" label="Submit"/>
        </s:form>
    </body>
</html> 

uploadFile.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>
    <package name="attribute" extends="struts-default">
        <action name="uploadAction" class="com.scrolls.fileupload.action.UploadImageAction" method="uploadImage">
        <interceptor-ref name="fileUpload">
        <param name="maximumSize">2097152</param>
        <param name="allowedTypes">
        image/png,image/gif,image/jpeg,image/pjpeg
        </param>
        </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">ImageUploadSuccess.jsp</result>
            <result name="input">index.jsp</result>
    </action>

    </package>
</struts>

UploadImageAction.java

package com.scrolls.fileupload.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

public class UploadImageAction extends ActionSupport implements ServletRequestAware {
private File userImage;
private String userImageContentType;
private String caption;

private HttpServletRequest servletRequest;

    public String uploadImage() {
        try {
            HttpSession session = servletRequest.getSession();
            ServletContext context = session.getServletContext();
            String filePath = context.getRealPath("/");
            System.out.println("Server path:" + filePath);

            File fileToCreate = new File(filePath, this.caption);

            FileUtils.copyFile(this.userImage, fileToCreate);

            } 
        catch (Exception e) {
        System.out.println(e);
        return INPUT;
        }

        return SUCCESS;
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

ImageUploadSuccess.jsp

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<h2>Struts2 Image Upload</h2>
<b>Image Uploaded To Folder :</b><s:property value="userImage"/>
<br/>
<b>Content Type:</b> <s:property value="userImageContentType"/>
<br/>
<b>Uploaded Image Name:</b> <s:property value="caption"/>
<br/>
<b>Uploaded Image Preview :</b>
<br/>
<img src="<s:property value="userImageFileName"/>"/>

</body>
</html>

例外:

SEVERE: Servlet.service() for servlet [default] in context with path [/fileupload] threw exception [java.lang.UnsupportedOperationException: Not supported yet.] with root cause
java.lang.UnsupportedOperationException: Not supported yet.
    at com.scrolls.fileupload.action.UploadImageAction.setServletRequest(UploadImageAction.java:81)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:131)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:268)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

3 个答案:

答案 0 :(得分:1)

... 您已经抛出该异常,正是堆栈跟踪告诉您您所在的位置。

您是否不了解Java堆栈跟踪?您是否无法阅读其提供的信息?

它指向您的代码。

答案 1 :(得分:0)

首先defaultStack已经包含fileUpload拦截器,因此您必须配置该拦截器,而不是将其两次添加到您的动作拦截器堆栈。

<action name="uploadAction" class="com.scrolls.fileupload.action.UploadImageAction" method="uploadImage">
    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">2097152</param>
        <param name="fileUpload.allowedTypes">
             image/png,image/gif,image/jpeg,image/pjpeg
        </param>
    </interceptor-ref>
    <result name="success">ImageUploadSuccess.jsp</result>
    <result name="input">index.jsp</result>
</action>

第二:在Struts2中,您不需要从请求中读取提交的文件。阅读文件上传http://struts.apache.org/2.x/docs/file-upload.html

答案 2 :(得分:0)