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]
[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]