我在上传文件时遇到nullpointer异常

时间:2013-10-29 09:22:23

标签: java file-upload struts2

嗨,从这段代码我得到空指针异常,当我要读取uploadfilename我正在努力解决这个问题,但这个问题是没有解决任何人PLZ帮我解决这个....

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.
    }

3 个答案:

答案 0 :(得分:0)

我建议从servlet上下文尝试getResourceAsStream而不是使用文件路径。这将适用于WAR文件。

您需要考虑的另一个问题是,除了您的测试用例之外,该文件甚至不太可能驻留在服务器上。对于远程用户,文件路径无意义。 “上传文件”将在不是服务器的计算机上发生。您的代码在这种情况下不正确。

答案 1 :(得分:0)

如果您已将应用程序部署为WAR,则代码将返回null

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

答案 2 :(得分: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;