错误报告是: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();
}
------解决思路----------------------
看给出的英文信息,文件名错误或者不存在.
------解决思路----------------------
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 上传。