webwork 文件上传
关键字: webwork 文件上传Webwork的文件上传是采用拦截器来实现的
下面我们按照从客户端到服务器端的顺序讲讲如何配置:
1.第一点要注意的是form表单的两个属性
method="post" enctype="multipart/form-data" 一定要正确设置
2.Action配置:上传文件的action要配置拦截器
<interceptor-ref name="fileUploadStack"></interceptor-ref>
UploadAction类:这个类中的方法名字跟form表单中文件上传输入筐关系很密切
//这3个字段是必须的
private File upload; //取得上传得文件
private String contentType; //文件类型
private String fileName; //原始文件名
public void setUpload(File file) {
this.upload = file;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
<form name="form1" method="post" action="adSave.action"
enctype="multipart/form-data">
上传文件:<input type="file" name="upload" >
</form>
注意这儿的输入框
name=’upload’ 对应UploadAction的public void setUpload(File file)
文件获取方法为public String getUploadFileName(),方法格式为getXXXFileName();
Webwork默认的是用commons fileupload工具上传,它需要一个临时目录 在webwork.properties中可以配置webwork.multipart.saveDir=/temp
处理程序为
protected void uploadFile(File uploadFile, String savePath, String fileName)throws IOException{
if (null != uploadFile) {
FileOutputStream outputStream = new FileOutputStream(savePath + fileName);
FileInputStream fileIn = new FileInputStream(uploadFile);
byte[] buffer = new byte[1024];
int len;
while ((len = fileIn.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
fileIn.close();
outputStream.close();
}
}
发表评论
- 浏览: 88155 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
共 2 张
最新评论
-
webwork ww:iterator 标签 ...
多谢!解决了一个问题~~
-- by huguohuan -
sso研究1-------跨域cook ...
你这不能算跨域,老大
-- by willfcareer2 -
刚上班一个月 遭遇新公司 ...
找工作的确不好受!现在想想找工作的那段时光都头大!
-- by ttxiangyou -
Js 事件冒泡
又受教了……不错,学习学习……
-- by xieboxin -
刚上班一个月 遭遇新公司 ...
够损,哈哈,加油~
-- by jizhuayazhua






评论排行榜