webwork 的上传文件功能

jsp页面代码:
[code]<form action="/ww/upload.action" name="myform2" enctype="multipart/form-data" method="POST">
<div align="center" >
<br>
<br>
<br>
<br>
<br>
<div class="upload" style="text-align:left;" style="width:260px" >
<input type="text" name="title"/>文章标题<br>
<input type="text" name="titleFile">文件名称<br>
<INPUT type="file" name="file" ><br>

</div>
<br>
<br>
<a href="#" onClick="tijiao();">上传</a>
</div>
</form>[/code]

xwork.xml配置:
[code]<action name = "upload" class = "com.iyouhe.test.UploadAction" >
<interceptor-ref name="fileUploadStack"></interceptor-ref>

<result name = "success" type = "dispatcher" >index.jsp</result >

<interceptor-ref name = "params" />

</action > [/code]
action处理:
[code]package com.iyouhe.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;

import com.iyouhe.sessionFactory.TitleDao;
import com.iyouhe.sessionFactory.vo.Title;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;

public class UploadAction implements Action {
private String title;
private String titleFile;
private File file;
String fileContentType;

public String execute() throws Exception {
// TODO Auto-generated method stub
ActionContext context=ActionContext.getContext();
Map map=context.getParameters();
context.getValueStack();
if(titleFile!=null)
{

FileOutputStream outputStream = new FileOutputStream("E:/myspace/aectwork/WebRoot/html/"
+ titleFile);
FileInputStream fileIn = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fileIn.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
fileIn.close();
outputStream.close();
}
Title title=new Title();
title.setTitle(getTitle());
title.setTitleFile(titleFile);
TitleDao tt=new TitleDao();
tt.addTitle(title);


return "success";

}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitleFile() {
return titleFile;
}
public void setTitleFile(String titleFile) {
this.titleFile = titleFile;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}


}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值