论坛首页 Java企业应用论坛

struts 2.3.4 文件上传,FileName,ContentType和File全为NULL,咋整

浏览 4852 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-07-09  
刚开始学Struts2,下午参照黑弓老师的视频写一个简单的文件上传模块,结果弄了一下午,File,FileName和ContentType全为null,实在搞不懂了,大家帮我看看,谢谢了!!
先贴代码:
Fileupload.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title><s:text name="loginPage" /></title> 
</head> 
<body> 
    <s:form action="fileUpload" method="post" enctype="multipart/form-data"> 
        选择文件: 
        <s:file name="upload"></s:file> 
        <s:submit></s:submit> 
    </s:form> 
</body> 
</html> 

----------------------------
Uploadaction.java代码

public class UploadAction extends ActionSupport { 
 
    private File upload; 
    private String uploadContentType; 
    private String uploadFileName; 
 
    public File getUpload() { 
        return upload; 
    } 
 
    public void setUpload(File upload) { 
        this.upload = upload; 
    } 
 
    public String getUploadContentType() { 
        return uploadContentType; 
    } 
 
    public void setUploadContentType(String uploadContentType) { 
        this.uploadContentType = uploadContentType; 
    } 
 
    public String getUploadFileName() { 
        return uploadFileName; 
    } 
 
 
    public void setUploadFileName(String uploadFileName) { 
        this.uploadFileName = uploadFileName; 
    } 
 
    public String execute() throws Exception { 
        System.out.println(uploadContentType + " : " + uploadFileName + "\n"); 
        FileInputStream fis = new FileInputStream(upload); 
        FileOutputStream fos = new FileOutputStream("D:/" + uploadFileName); 
        byte[] buffer = new byte[1024]; 
        int length = 0; 
        while((length = fis.read(buffer)) != -1) { 
            fos.write(buffer, 0, length); 
        } 
        fis.close(); 
        fos.close(); 
        return null; 
    } 


------------------------
Struts.xml代码

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
    "struts-2.3.dtd"> 
<struts> 
    <constant name="struts.devMode" value="true"></constant> 
    <constant name="struts.configuration.xml.reload" value="true"></constant> 
    <constant name="struts.custom.i18n.resources" value="mess"></constant> 
    <constant name="struts.i18n.encoding" value="UTF-8"></constant> 
    <constant name="struts.multipart.saveDir" value="/tmp"></constant> 
 
    <package name="lee" extends="struts-default"> 
        <action name="fileUpload" class="com.tt.www.action.UploadAction"> 
            <result name="input">/fileUpload.jsp</result> 
        </action> 
        <action name="login" class="com.tt.www.action.LoginAction"> 
            <result name="input">/login.jsp</result> 
            <result name="success">/success.jsp</result> 
            <result name="error">/error.jsp</result> 
        </action> 
    </package> 
</struts> 

----------------------
导入的Jar包有:




-----------------------
错误提示为:
Struts Problem Report

Struts has detected an unhandled exception:

Messages:
File: java/io/FileInputStream.java
Line number: 103
Stacktraces

java.lang.NullPointerException
    java.io.FileInputStream.(FileInputStream.java:103)
    com.abchina.www.action.UploadAction.execute(UploadAction.java:19)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:320)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    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:249)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:619)
  • 大小: 30 KB
   发表时间:2012-07-09  
各位大虾,指点一下啊~
0 请登录后投票
   发表时间:2012-07-18  
Fileupload.jsp代码提交后,执行对应的UploadAction类中execute()方法,但作者代码中该方法的返回值都为null,无论成功与否,所以找不到对应的.jsp文件,.jsp文件也找不到对应属性的值,所以全为空。你应该把execute()的返回值改为input,以至找到对应的fileUpload.jsp
0 请登录后投票
   发表时间:2012-08-09  
楼上说得对啊..你看下你的返回值.return null如果你return null又想返回的话,用javascript的方法去写直接跳转,就可以返回一个null,而且能成功跳转
0 请登录后投票
   发表时间:2012-08-15  
你可以用一个ajaxfileupload的jquery插件来实现,结合json就可以了
0 请登录后投票
   发表时间:2012-08-15  
跟返回值是null没关系的

你是不是根本就没有 浏览文件 添加文件路径   直接提交了?
---------------------------------------------------------
  SUCCESS:Action正确的执行完成,返回相应的视图;
  NONE:表示Action正确的执行完成,但并不返回任何视图;
  ERROR:表示Action执行失败,返回到错误处理视图;
INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图
0 请登录后投票
   发表时间:2012-12-06  
java.lang.NullPointerException
都说了是空指针错误,断点调试一下,看看那里null
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics