-1

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

索引.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> 

上传文件.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)
4

3 回答 3

1

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

您不知道 Java 堆栈跟踪吗?您无法阅读它提供的信息吗?

它指向您的代码。

于 2012-11-12T14:51:43.883 回答
0

First of all defaultStack already includes fileUpload interceptor so you have to configure that interceptor instead of adding it twice to your action interceptors stack.

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

Second: in Struts2 you do not need to read submitted file from request. Read about file upload http://struts.apache.org/2.x/docs/file-upload.html.

于 2012-11-12T11:56:11.823 回答
0

您可以尝试从此链接下载示例项目。

http://javapostsforlearning.blogspot.com/2012/07/file-upload-in-struts-2.html

我已经尝试过了,它可以工作。(感谢教程创建者)

于 2014-03-07T04:38:56.447 回答