2014-12-03 35 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