2 ffw2008 FFW2008 于 2016.05.09 18:04 提问

struts2中的commons-fileupload组件上传文件时候遇到的错误

action如下:

public class UploadAction {
private File upload;// 封装上传文件
private String uploadContentType;// 封装上传文件的类型
private String uploadFileName;// 封装上传文件名的属性
private String uploadPath;// 封装上传文件在服务器中的路径,通过参数设置
private String result;// 封装处理结果的值
private String valiPdf;

public File getUpload() {
    return upload;
}

public void setUpload(File upload) {
    this.upload = upload;
}

public String getUploadContentType() {
    return uploadContentType;
}

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

public String getUploadFileName() {
    return uploadFileName;
}

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

public String getUploadPath() {
    return uploadPath;
}

public void setUploadPath(String uploadPath) {
    this.uploadPath = uploadPath;
}

public String getResult() {
    return result;
}

public void setResult(String result) {
    this.result = result;
}

public String getValiPdf() {
    return valiPdf;
}

public void setValiPdf(String valiPdf) {
    this.valiPdf = valiPdf;
}

public String execute() throws IOException,SQLException{
    String fn = uploadPath + uploadFileName;
    String classPath = this.getClass().getResource("").getPath();
    String webPath = new File(new File(new File(new File(new File(classPath).getParent()).getParent()).getParent()).getParent()).getParent();
    String webFn = webPath + "\\" + fn;
    System.out.println(uploadFileName+"........................");
    String[] strsplit = uploadFileName.split("\\.");
    for(String str: strsplit){
        System.out.println(str+"........................");
    }
    if(!("pdf".equalsIgnoreCase(strsplit[1]))){
        valiPdf = "文档格式不对,应该为PDF格式文件";
        return "fail";
    }else{              
        if (new File(webFn).exists()) {
            result = "该文件已存在";
        } else {
            Connection conn = null;
            PreparedStatement ps = null;
            conn = JDBCUtils.getConn();
            ps = conn.prepareStatement("insert into address values(null,'"+fn+"')");
            ps.executeUpdate();
            JDBCUtils.close(conn, ps, null);

            FileOutputStream fos = new FileOutputStream(webFn);
            InputStream is = new FileInputStream(upload);
            byte[] buffer = new byte[102400];
            int count = 0;
            while ((count = is.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            fos.close();
            is.close();
            result = "文件上传成功";
        }
        return "result";
    }
}

}

struts.xml代码如下:
图片说明

为什么去掉if语句

if(!("pdf".equalsIgnoreCase(strsplit[1]))){
valiPdf = "文档格式不对,应该为PDF格式文件";
return "fail";

数据就可以传进去,不去掉的话,就传不进去呀??

难道是错误的result标签不可以配吗

3个回答

fk002008
fk002008   2016.05.09 19:15
已采纳

写错了哦
注意

<param>要写在 <result>里面
<result>
    <param>
</result>
fk002008
fk002008   2016.05.09 18:50

你写的配置文件看不到啊

FFW2008
FFW2008   2016.05.09 19:01




/WEB-INF/page/result.jsp
upload\
/WEB-INF/page/fileUpload.jsp




/WEB-INF/page/fileUpload.jsp




/WEB-INF/page/show.jsp


配置文件是这个

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!