struts2文件上传(Action方法)
2009-07-20 21:07
323 查看
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
private File file;
//为名称之后加FileName,不得更改
private String fileFileName;
//同上
private String fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
InputStream is=new FileInputStream(file);
String root=ServletActionContext.getRequest().getRealPath("/upload");
File destFile=new File(root,this.getFileFileName());
OutputStream os=new FileOutputStream(destFile);
byte[] buffer=new byte[400];
int len=0;
while((len=is.read(buffer))>0){
os.write(buffer, 0, len);
}
os.close();
is.close();
return SUCCESS;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String username;
private String password;
private File file;
//为名称之后加FileName,不得更改
private String fileFileName;
//同上
private String fileContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception {
InputStream is=new FileInputStream(file);
String root=ServletActionContext.getRequest().getRealPath("/upload");
File destFile=new File(root,this.getFileFileName());
OutputStream os=new FileOutputStream(destFile);
byte[] buffer=new byte[400];
int len=0;
while((len=is.read(buffer))>0){
os.write(buffer, 0, len);
}
os.close();
is.close();
return SUCCESS;
}
}
相关文章推荐
- struts2中action的方法里怎样判断用户是否选择了上传文件
- struts2上传文件,action接收到文件对象为null的解决方法
- struts2文件上传,看到书上讲的action类的执行方法不是execute而是upload,不解,故搜之
- Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法
- struts2-core-2.1.8.1修改文件上传的方法
- Struts2 commons-fileupload 在上传2M以上文件出现异常解决方法
- 用struts2 完成一个简单的通讯录,实现增删改查,分页,文件上传(头像),表单验证(两种方法)
- struts2 action接收文件上传数据处理
- Struts2 上传文件方法
- struts2 上传文件在action 里的属性 name
- jquery之与struts2整合异步上传文件时弹出下载对话框的解决方法
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- struts2上传文件后 return SUCCESS失败的处理方法!
- struts2中文件上传文件大小限制以及解决方法(common-FileUpload)
- 在struts2下不刷新上传excel文件,并回调页面方法!
- struts2 上传文件 Action.setFileupload([Ljava.lang.String;)
- 关于struts2上传文件报错“No result defined for action and result input”
- fckedit和struts2集成不能上传文件的解决方法
- Struts2修改上传文件大小限制方法解析