FFW2008 2016-05-09 10:04 采纳率: 47.1%
浏览 1010
已采纳

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条回答

  • lambda-fk 2016-05-09 11:15
    关注

    写错了哦
    注意

    <param>要写在 <result>里面
    <result>
        <param>
    </result>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题