2014-12-03 77 views
1

我想使用struts2上传2个文件......但是当我在FileInputStream中打开文件时,我得到一个NullPointerException,因为文件值被指定为Null。Struts 2文件上传给NULL指针

Upload.html

<form action="report.action" method="get" enctype="multipart/form-data"> 
<div class="form-group input-group"> 
    <div class="input-group"> 
     <span class="input-group-btn"> 
     <span class="btn btn-primary btn-file">Browse&hellip; <input type="file" name="file1"></span></span> 
      <input type="text" class="form-control" readonly placeholder="Amdocs Report"> 
    </div> 
</div> 

<div class="form-group input-group"> 
    <div class="input-group"> 
     <span class="input-group-btn"> 
     <span class="btn btn-primary btn-file">Browse&hellip; <input type="file" name="file2"></span></span> 
     <input type="text" class="form-control" readonly placeholder="ILC Report"> 
    </div> 

Action类

private File file1; 
private File file2; 

public File getFile1() { 
    return file1; 
} 

public void setFile1(File file1) { 
    this.file1 = file1; 
} 

public File getFile2() { 
    return file2; 
} 

public void setFile2(File file2) { 
    this.file2 = file2; 

} 
public String execute() { 

    try { 
    FileInputStream file= new FileInputStream(file1); 
    System.out.println(file1); 
    System.out.println(file2); 
    generate(file1,file2); 
    return "success"; 
    }catch(Exception ex) { 
     ex.printStackTrace(); 
     return "success"; 
    } 
} 

struts.xml的

<struts> 
<package name="default" extends="struts-default"> 
    <action name="report" class="com.o2.report.GenerateReport"> 
     <interceptor-ref name="fileUpload"/> 
     <interceptor-ref name="basicStack"/> 
     <result name="success">index.html</result> 
    </action> 

</package> 

我得到的文件1和文件2的值作为空当我看到调试模式。

错误

[3/12/14 16:30:45:994 IST] 0000001d SystemErr  R java.lang.NullPointerException 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at java.io.FileInputStream.<init>(FileInputStream.java:109) 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at com.o2.report.GenerateReport.execute(GenerateReport.java:39) 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45) 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37) 
[3/12/14 16:30:45:995 IST] 0000001d SystemErr  R  at java.lang.reflect.Method.invoke(Method.java:599) 
[3/12/14 16:30:45:996 IST] 0000001d SystemErr  R  at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) 
[3/12/14 16:30:45:996 IST] 0000001d SystemErr  R  at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) 
[3/12/14 16:30:45:996 IST] 0000001d SystemErr  R  at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) 
[3/12/14 16:30:45:996 IST] 0000001d SystemErr  R  at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167) 

回答

1

你正在做很多奇怪的事情。

  1. 使用POST的方法,因为你需要一个身体(和你不执行幂等操作... GET =读,POST =写,基本上,虽然他们往往可以使用的其他方式);

  2. 使用defaultStack,或者定义你的Stack;

  3. 与文件一起,您应该为contentyType和filename定义两个字符串;

  4. 您可以从单个<input type="file" />元素中选择upload multiple files easy with the multiple attribute;

  5. 但即使您要使用多个元素,仍可以将文件发布到List而不是不同的文件对象。如果明天yuo需要上传10个文件呢?或100?每次您都需要来这里,更改代码并发布它。

  6. 不要从catch块中返回SUCCESS而不会在addActionError("Something bad happened");中添加错误。

+0

对不起,奇怪的事情只是为了检查文件流为什么给出空指针错误。更改为defaultStack和方法以解决问题。谢谢 – 2014-12-03 11:36:23