0

在这里我正在上传文件,我想读取文件类型和文件扩展名,但是当我给文件时,FileUtils.copyFile(this.uploadFile, fileToCreate); 附近出现空指针异常;有人可以解决这个问题吗...

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

1

这是该代码在不到 24 小时内的第三个问题……这是由在线北极星拍摄的吗?请问可以在这里举报吗?

BTW,主要问题是在使用Struts2上传文件时,通过fileUploadInterceptor,你必须尊重以下结构

private File   <filevarname>;
private String <filevarname>FileName;
private String <filevarname>ContentType;

所以如果你<filevarname>是“uploadFile”,你应该有

private File uploadFile;
private String uploadFileFileName;
private String uploadFileContentType;

如果您将其更改为“上传”,则应为

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

这对我来说更具可读性。

于 2013-10-29T11:44:40.677 回答
0

检查是否将操作类中的内容类型字段从uploadContentTypeto重命名uploadFileContentType。还将 更改uploadFileNameuploadFileFileName

更改相应的 getter 和 setter。我不确定它是否会导致问题。所以这更像是一种试错建议。

更新:看来您正在获得 NPE on GET 操作。

在您的尝试块之前添加此代码:

if(!servletRequest.getMethod().equalsIgnoreCase("POST")){
   return INPUT;
}

这不是很优雅,但应该适用于这个例子。我已经检查过了。:)

于 2013-10-29T05:39:11.933 回答
0

请检查;

  1. 关于 ActionServlet 的 html 表单中的字段名称
  2. 使用 html-form 检查 enc-type
于 2013-10-29T04:16:41.403 回答