2013-10-29 85 views
-1

嗨,從這段代碼我得到空指針異常,當我要讀uploadfilename時,我正在嘗試很多來解決這個問題,但這個問題不解決任何人都可以幫我解決在這....我在上傳文件時出現空指針異常

public class ImportAction extends ActionSupport implements ServletRequestAware { 

    private static final long serialVersionUID = 1L; 
    private File uploadFile; 
    private String uploadfileFileName; 
    private String uploadfileContentType; 
    private HttpServletRequest servletRequest; 

    public String getUploadfileFileName() { 
     return uploadfileFileName; 
    } 

    public void setUploadfileFileName(String uploadfileFileName) { 
     this.uploadfileFileName = uploadfileFileName; 
    } 

    public String getUploadfileContentType() { 
     return uploadfileContentType; 
    } 

    public void setUploadfileContentType(String uploadfileContentType) { 
     this.uploadfileContentType = uploadfileContentType; 
    } 

    public File getUploadFile() { 
     return uploadFile; 
    } 

    public void setUploadFile(File uploadFile) { 
     this.uploadFile = uploadFile; 
    } 
    RegisterDAO dao = new RegisterDAO(); 

    /** 
    * 
    * @return 
    * @throws 
    * Exception 
    */ 
    @SuppressWarnings("unchecked") 
    @Override 
    public String execute() throws Exception { 
     String filePath = servletRequest.getSession().getServletContext().getRealPath("/"); 
     System.out.println("filepath" + filePath); 
     System.out.println("file" + getUploadFile()); 
     System.out.println("filename" + getUploadfileFileName()); 
     File fileToCreate = new File(filePath, this.uploadfileFileName); 
     try { 
      FileUtils.copyFile(this.uploadFile, fileToCreate); 
     } catch (IOException e) { 
      addActionError(e.getMessage()); 
     } 
     return SUCCESS; 


//  Model m = new Model(); 
//  String result = m.fileimport(uploadFile, uploadfileFileName); 
//  String result = m.fileimport(); 
//  if (result.equals(SUCCESS)) { 
//   return SUCCESS; 
//  } else { 
//   addActionError(getText(result)); 
//   return "failure"; 
//  } 
    } 

    @Override 
    public void validate() { 
     System.out.println("inside validate of LA"); 
    } 

    @Override 
    public void setServletRequest(HttpServletRequest servletRequest) { 
     this.servletRequest = servletRequest; //To change body of generated methods, choose Tools | Templates. 
    } 
+0

可能重複[文件路徑中上傳附近的文件空指針異常(http://stackoverflow.com/questions/19634677/in-uploading-file-null-pointer-例外 - 接近文件路徑) –

+0

重複,並且已經回答。您尚未更正變量名稱。至少閱讀我們給你的答案....... –

回答

0

我建議嘗試getResourceAsStream從servlet上下文,而不是使用文件路徑。這將與WAR文件一起工作。

您需要考慮的另一個問題是,文件甚至不可能駐留在除測試用例之外的服務器上。對於遠程用戶來說,文件路徑毫無意義。 「上傳文件」將發生在不是服務器的機器上。你的代碼在這種情況下是不正確的。

+0

從這段代碼我想要擴展閱讀它就可以了。 – user2838630

+0

我需要得到的文件名是沒有得到它.. – user2838630

+0

不,你不能。只是InputStream。您的要求不清楚。 – duffymo

0

如果您已部署爲WAR下面的代碼應用程序將返回null

String filePath = servletRequest.getSession().getServletContext().getRealPath("/"); 
+0

如何克服它.. – user2838630

+0

我已經告訴你 - getResourceAsStream。 – duffymo

+0

@duffymo是正確的使用'getResourceAsStream' – Shamse

0
private File uploadFile; 
    private String uploadfileFileName; 
    private String uploadfileContentType; 

這是錯誤的,注意在其他性質的小f,它應該是F,因爲文件場名字是uploadFile

這是有效的:

private File upload; 
    private String uploadFileName; 
    private String uploadContentType; 

這一個也是正確的:

private File uploadFile; 
    private String uploadFileFileName; 
    private String uploadFileContentType; 
相關問題