当我尝试使用eclipse中的Struts上传和保存图像文件时,会将文件名显示为null [英] When I try to upload and save an image file using Struts in eclipse it shows the file name as null

查看:183
本文介绍了当我尝试使用eclipse中的Struts上传和保存图像文件时,会将文件名显示为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的索引页:

 <%@ page language =java contentType =text / html; charset = ISO-8859-1
pageEncoding =ISO-8859-1%>
<%@ taglib prefix =suri =/ struts-tags%>
< html>
< head>
< meta http-equiv =SContent-Typecontent =text / html; charset = ISO-8859-1>
< title>在此插入标题< / title>
< / head>
< body>
< s:form action =clickuploadmethod =postenctype =multipart / form-data>
< s:file name =myFile>< / s:file>
< s:submit value =上传/>
< / s:form>
< / body>
< / html>

struts.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE struts PUBLIC
- // Apache Software Foundation // DTD Struts配置2.3 // EN
http://struts.apache.org/dtds/struts- 2.3.dtd>
< struts>
< include file =struts-default.xml/>
< constant name =struts.enable.DynamicMethodInvocationvalue =false/>
< constant name =struts.devModevalue =true/>
< package name =uploadSnewnamespace =/extends =struts-default>
< action name =clickuploadclass =tryupload.UploadNewFilemethod =execute>
< interceptor-ref name =basicStack/>
< interceptor-ref name =fileUpload>
< param name =maximumSize> 2048000< / param>
< param name =allowedTypes> image / jpeg,image / gif< / param>
< / interceptor-ref>
< result name =success> /success.jsp< / result>
< result name =error> /error.jsp< / result>
< result name =input> /index.jsp< / result>
< / action>
< / package>
< / struts>

当我上传文件并提交时显示:


$ b $


消息:
文件:java / io / File.java
行号:291


------------------------------ --------------------------------------------------

堆栈跟踪

  java.lang.NullPointerException 
java.io.File。< init>(File.java:291)
tryupload.UploadNewFile.execute(UploadNewFile.java:32)
(本地方法)
b java.lang.reflect.Method.invoke(Method.java:606)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.ja va:870)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor。 XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
ognl.ASTMethod.getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.Ognl.getValue(Ognl.java:494)
ognl.Ognl.getValue(Ognl.java:458)
com.opensymphony.xwork2.ognl.OgnlUtil $ 2.execute(OgnlUtil.java :309)
com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.ja va:307)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
com。 opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:326)
com.opensymphony.xwork2.DefaultActionInvocation.invoke( DefaultActionInvocation.java:244)
org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
c om.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2。 DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor。 java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
c om.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
com.opensymphony.xwork2.DefaultActionInvocation。 invoke(DefaultActionInvocation.java:244)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java: 98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com。 opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
com.opensymphony。 xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction( Dispatcher.java:564)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
org。 apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.core。 StandardHostValve.invoke(StandardHostValve.java:142)
org.apache.catalina.valves.Erro rReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve。 java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:668)
org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1527)
org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1484)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util。 concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread $ WrappingRun nable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:724)




你是看到此页面,因为开发模式已启用。开发模式或devMode支持额外的调试行为和报告来协助开发人员。要禁用此模式,请在您的WEB-INF / classes / struts.properties文件中设置:

struts.devMode = false


我的控制台:

2015年9月30日10:16:29 PM org.apache.struts2.interceptor.FileUploadInterceptor警告
警告:内容类型不允许:myFileFiveFingerDeathPunch image_
文件名:null

UploadNewFile - 我的动作类:

  package tryupload; 

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;


public class UploadNewFile extends ActionSupport implements ServletRequestAware {
/ **
*
* /

private static final long serialVersionUID = 1L;
private HttpServletRequest servletRequest;
私人文件myFile;
private String myFileContentType;
private String myFileFileName;

public File getMyFile(){
return myFile;
}
public void setMyFile(File myFile){
this.myFile = myFile;
}
public String getMyFileFileName(){
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName){
this.myFileFileName = myFileFileName;

}
public String getMyFileContentType(){
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType){
this.myFileContentType = myFileContentType;
}


public String execute()throws异常{

/ * File saveFilePath = new File(WebContent / images+ myFileFileName);
System.out.println(Filename:+ myFileFileName);
* /
try {
String filePath = servletRequest.getSession()。getServletContext()。getRealPath(/);
System.out.println(Server path:+ filePath);
System.out.println(Filename:+ myFileFileName);

文件fileToCreate = new File(filePath,this.myFileFileName);
FileUtils.copyFile(this.myFile,fileToCreate);
}
catch(IOException ex){
System.out.println(无法保存文件:+ ex.getMessage());
ex.getStackTrace();
addActionError(ex.getMessage());

return ERROR;
}
return SUCCESS;
}

@Override
public void setServletRequest(HttpServletRequest servletRequest){
// TODO自动生成的方法存根
this.servletRequest = servletRequest;

}

}


解决方案

为了正确处理多部分请求,FileUpload拦截器必须在其他拦截器之前运行以阻止其他拦截器。然后从

 < interceptor-ref name =basicStack/> 
< interceptor-ref name =fileUpload>
< param name =maximumSize> 2048000< / param>
< param name =allowedTypes> image / jpeg,image / gif< / param>
< / interceptor-ref>

 < interceptor-ref name =fileUpload> 
< param name =maximumSize> 2048000< / param>
< param name =allowedTypes> image / jpeg,image / gif< / param>
< / interceptor-ref>
< interceptor-ref name =basicStack/>

足以让它工作。 阅读更多


This is my index page:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
 <%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
 <head>
 <meta http-equiv="SContent-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
 <s:form action="clickupload" method="post" enctype="multipart/form-data" >
 <s:file name="myFile"></s:file>
 <s:submit value="Upload"/>
 </s:form> 
 </body>
 </html>

struts.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
 <struts>
   <include file="struts-default.xml"/>
   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
   <constant name="struts.devMode" value="true" />
   <package name="uploadSnew" namespace="/" extends="struts-default">
     <action name="clickupload" class="tryupload.UploadNewFile" method="execute">
         <interceptor-ref name="basicStack"/>
        <interceptor-ref name="fileUpload">
            <param name="maximumSize">2048000</param>
            <param name="allowedTypes">image/jpeg,image/gif</param >
            </interceptor-ref>
             <result name="success" >/success.jsp</result>
             <result name="error">/error.jsp</result>
             <result name="input">/index.jsp</result>
     </action>
   </package>
 </struts>

When I upload a file and submit it shows:

     Struts Problem Report
     Struts has detected an unhandled exception: 

     Messages:  
     File: java/io/File.java 
     Line number: 291 


     --------------------------------------------------------------------------------

Stacktraces

     java.lang.NullPointerException 
         java.io.File.<init>(File.java:291)
         tryupload.UploadNewFile.execute(UploadNewFile.java:32)
         sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
         sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         java.lang.reflect.Method.invoke(Method.java:606)
         ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
         ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
         ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
         com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
         com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
         ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
         ognl.ASTMethod.getValueBody(ASTMethod.java:90)
         ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
         ognl.SimpleNode.getValue(SimpleNode.java:258)
         ognl.Ognl.getValue(Ognl.java:494)
         ognl.Ognl.getValue(Ognl.java:458)
         com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)
         com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
         com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307)
         com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
         com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
         org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:326)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
         com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
         com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
         com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
         com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
         org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
         org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
         org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
         org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
         org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
         org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
         org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
         org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
         org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
         org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
         org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
         org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
         org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
         org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
         org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
         org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
         org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
         java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
         java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
         org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
         java.lang.Thread.run(Thread.java:724)




     You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set: 

     struts.devMode=false

     in your WEB-INF/classes/struts.properties file. 
      My console:

     Sep 30, 2015 10:16:29 PM org.apache.struts2.interceptor.FileUploadInterceptor warn
     WARNING: Content-Type not allowed: myFile "FiveFingerDeathPunch.jpg" "upload_1a24317f_cf06_45b2_be8d_1b56d56eab1e_00000000.tmp" image/pjpeg
     Server path:E:\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\tryupload\
     Filename:null

UploadNewFile - My action class:

package tryupload;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;  


public class UploadNewFile extends ActionSupport implements ServletRequestAware {
    /**
     * 
     */

    private static final long serialVersionUID = 1L;
    private HttpServletRequest servletRequest; 
    private File myFile;
    private String myFileContentType;
    private String myFileFileName;

    public File getMyFile() {
        return myFile;
        }
    public void setMyFile(File myFile) {
        this.myFile = myFile;
        }
    public String getMyFileFileName() {
        return myFileFileName;
        }
        public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;

        }
    public String getMyFileContentType() {
        return myFileContentType;
        }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
        }


    public String execute() throws Exception {

        /*File saveFilePath = new File("WebContent/images" +myFileFileName);
        System.out.println("Filename:"+myFileFileName);
*/
    try {
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");  
          System.out.println("Server path:" + filePath);  
        System.out.println("Filename:"+myFileFileName);

          File fileToCreate = new File(filePath, this.myFileFileName);  
            FileUtils.copyFile(this.myFile, fileToCreate);
        } 
        catch (IOException ex) {
            System.out.println("Couldn't save file: " + ex.getMessage());
            ex.getStackTrace();
            addActionError(ex.getMessage());  

            return ERROR;
        }
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        // TODO Auto-generated method stub
        this.servletRequest = servletRequest;  

    }

}

解决方案

To handle the multipart request correctly, the FileUpload Interceptor must run before other interceptors that otherwise would mess things up. Then changing the order from

<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
    <param name="maximumSize">2048000</param>
    <param name="allowedTypes">image/jpeg,image/gif</param >
</interceptor-ref>

to

<interceptor-ref name="fileUpload">
    <param name="maximumSize">2048000</param>
    <param name="allowedTypes">image/jpeg,image/gif</param >
</interceptor-ref>
<interceptor-ref name="basicStack"/>

is enough to make it work. Read more.

这篇关于当我尝试使用eclipse中的Struts上传和保存图像文件时,会将文件名显示为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆