在struts2中集成fileuoload功能,因为在导入的jar包中包含了common-fileipload.jar文件
在struts2中的interceptor 中有一个fileupload拦截器,他的主要功能就是完成文件上传。
注意事项
- method=post
- 所有组件必须有name
- encType=multipart/form-data
Struts2上传步骤
- 在页面上创建表单
- 创建Action,在action中定义三个变量用来接收上传信息
- 使用FileUtils的copyFile方法完成文件复制操作
FileUploadAction.java
package com.qwl.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID=1L;
//提交过来的文件
private File file;
//提交过来的file的名字
private String fileFileName;
//提交过来的file的类型
private String fileContentType;
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;
}
public String execute()throws Exception{
/*System.out.println(file.getCanonicalPath());
System.out.println(this.fileContentType);
System.out.println(this.fileFileName);*/
//流操作
/*//文件输入流
InputStream is=new FileInputStream(file);
//设置文件的保存目录
String uploadPath=ServletActionContext.getServletContext().getRealPath("/upload");
//设置目标文件
File toFile=new File(uploadPath,this.getFileFileName());
//文件输出流
OutputStream os=new FileOutputStream(toFile);
byte[] buffer=new byte[1024];
int length=0;
//读取file文件输出到toFile文件中
while(-1!=(length=is.read(buffer,0,buffer.length)))
{
os.write(buffer);
}
//关闭输入流和输出流
is.close();
os.close();*/
writeFile();
return SUCCESS;
}
public void writeFile() throws IOException{
FileUtils.copyFile(file,new File("/upload",fileFileName));
}
}
}
fileUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="file" label="文件上传"></s:file>
<s:submit value="上传"/>
<s:reset value="重置"/>
</s:form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<s:form action="fileUpload" method="post" enctype="multipart/form-data">
<s:file name="file" label="文件上传"></s:file>
<s:submit value="上传"/>
<s:reset value="重置"/>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="struts2" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.qwl.action.FileUploadAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>