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

3 回答 3

0

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

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

于 2013-10-29T09:23:52.713 回答
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;
于 2013-10-31T15:08:28.590 回答
0

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

String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
于 2013-10-29T09:31:01.970 回答