0

ここでファイルをアップロードしていて、ファイルの種類とファイル拡張子を読みたかったのですが、ファイルを指定すると FileUtils.copyFile(this.uploadFile, fileToCreate); の近くで nullpointer 例外が発生しています。誰でもこの問題を解決できますか...

public class ImportAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;
    private File uploadFile;
    private String uploadFileName;
    private String uploadContentType;
    private HttpServletRequest servletRequest;

    public File getUploadFile() {
        return uploadFile;
    }

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

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    RegisterDAO dao = new RegisterDAO();

    /**
     *
     * @return
     * @throws
     * Exception
     */
    @SuppressWarnings("unchecked")
    @Override
    public String execute() throws Exception {
        try {
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, this.uploadFileName);
            FileUtils.copyFile(this.uploadFile, fileToCreate);
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
            return INPUT;
        }
        Model m = new Model();
        String result = m.fileimport(uploadFile, uploadFileName);
        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 hsr) {
        this.servletRequest = hsr; //To change body of generated methods, choose Tools | Templates.
    }
}

これは私のjspページです

<s:form method="post" action="file" enctype="multipart/form-data">
            <s:file label="choose file" name="uploadFile"></s:file>
            <s:submit></s:submit>
        </s:form>

ファイルパスを次のように出力しました filepathD:\ranjith\ranjithdemo3\build\web\

4

3 に答える 3