源码如下
package com.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File upload;
private String uploadFileName;
public File getUpload(){
return upload;
}
public void setUpload(File upload){
this.upload=upload;
}
public String uploading() throws Exception{
InputStream is=new FileInputStream(getUpload());
OutputStream os=new FileOutputStream("e:\\upload\\"+uploadFileName);
byte buffer[]=new byte[1024];
int count=0;
while((count=is.read(buffer))>0){
os.write(buffer, 0, count);
}
os.close();
is.close();
return SUCCESS;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
页面主要代码:
<s:form action="upload.action" method="post" enctype="multipart/form-data">
<s:file name="upload.uploadFileName" label="上传的文件"></s:file>
<s:submit value="上传 "></s:submit>
</s:form>
运行一上传文件,控制台就会提示
警告: Error setting expression 'upload.uploadFileNameFileName' with value '[Ljava.lang.String;@7d0ade7e'
ognl.OgnlException: target is null for setProperty(null, "uploadFileNameFileName", [Ljava.lang.String;@7d0ade7e)
------解决方案--------------------
<s:file name="upload.uploadFileName"这样写,代表upload是一个对象,且这个对象有uploadFileName属性,去掉upload.