上传文件有错误

lliiqiang 2015-03-13 01:37:34
错误报告是:java.lang.IllegalArgumentException: Files' name is invalid or does not exist (1205).
选择文件页面:
<script type="text/javascript">
function startclick(){
if(document.getElementById("startdate").value==""){
alert("请输入开始时间");
return;
}
if(document.getElementById("stopdate").value==""){
alert("请输入终止时间");
return;
}
if(document.getElementById("file").value==""){
alert("请选择文件");
return;
}
document.getElementById("form").submit();
}
</script>
<body>
<form id="form" action="excel.jsp" method="post" enctype="multipart/form-data">
开始时间:<input type="date" name="startdate" id="startdate"/>终止时间<input name="stopdate" type="date" id="stopdate"/>
选择文件:<input type="file" id="file" name="file"/>
<input type="button" value="确定" onclick="javascript:startclick()"/>
</form>
文件处理页面:
com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.SmartUpload();
//上传初始化
try{
mySmartUpload.initialize(pageContext);
mySmartUpload.setMaxFileSize(10*1024*1024);
//设置所有文件最大容量
mySmartUpload.setTotalMaxFileSize(100*1024*1024);
mySmartUpload.setAllowedFilesList("xls");
//上传
mySmartUpload.upload();//获取文件
com.jspsmart.upload.Files myfile = mySmartUpload.getFiles();
System.out.println(myfile.getCount());
com.jspsmart.upload.File file=myfile.getFile(0);//在这里出错,上面打印文件个数为0.
// file.saveAs("D://aaa.xls");
}catch(Exception e){
e.printStackTrace();
}
...全文
187 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Defonds 2015-03-16
  • 打赏
  • 举报
回复
1. 用 s2 上传:
package com.ljq.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
    
    private File image; //上传的文件
    private String imageFileName; //文件名称
    private String imageContentType; //文件类型

    public String execute() throws Exception {
        String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
        System.out.println("realpath: "+realpath);
        if (image != null) {
            File savefile = new File(new File(realpath), imageFileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(image, savefile);
            ActionContext.getContext().put("message", "文件上传成功");
        }
        return "success";
    }

    public File getImage() {
        return image;
    }

    public void setImage(File image) {
        this.image = image;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    public String getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }

    
}
2. 或者不要 s2 上传,使用 servlet 上传。
lliiqiang 2015-03-16
  • 打赏
  • 举报
回复
经过测试发现type="file"表单必须有name,但是在单独的项目中可以,在公司项目中不行。 后来验证确实和系统struts拦截器,web.xml配置文件中的 <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> //这里为了保密,隐藏了信息 </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 有关,因为去除它就可以上传,谁知道这个是什么拦截器,作用是什么.
言月秋 2015-03-14
  • 打赏
  • 举报
回复
看给出的英文信息,文件名错误或者不存在.

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧