需要用两个包(一般包含在struts2必须的包里面):一个是commons-fileupload-1.3.2.jar,另一个是commons-io-2.2.jar
步骤:
第一步:做界面。
1.<input type="file">
2.<form enctype="multipart/form-data">
<h1>文件上传</h1> <form action="Upload_upload" method="post" enctype="multipart/form-data"><!--固定格式,必须要有--> 标题:<input type="text" name="title"><br> 附件:<input type="file" name="file"><br><!--上传按钮--> <input type="submit" value="submit"> </form>
第二步:做Action。
三个成员:1.File类型 2.String fileFileName属性名 3.String fileContentType属性名
法一(推荐):org.apache.commons.io.FileUtils.copyFile很简单
import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private File file; private String fileFileName;//上传的文件名 private String fileContentType;//上传的文件类型 .. FileUtils.copyFile(srcFile, destFile);//源文件,目的文件 }
法二: 用IO流也可操作
InputStream is = new FileInputStream(file); OutputStream os = new FileOutputStream(new File(root, fileFileName)); byte[] buffer = new byte[500]; int length = 0; while(-1 != (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close();
实例:上传一个文件
1.做界面
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h1>文件上传</h1> <form action="Upload_upload" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"><br> 附件:<input type="file" name="file"><br> <input type="submit" value="submit"> </form> </body> </html>
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h1>上传成功</h1> </body> </html>
2.做Action
import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private String title; private File file; private String fileFileName; private String fileContentType; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 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 input() { return SUCCESS; } public String upload() throws IOException { //随机数 Random rand = new Random(); //给随机数一个范围 int n = rand.nextInt(9999); //格式化,缺失位补0 DecimalFormat df = new DecimalFormat("0000"); //把n放到format中格式化 String no = df.format(n); //日期格式化器(年月日时分秒毫秒) SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmsssss"); //获取当前时间 Date now = Calendar.getInstance().getTime(); //把当前时间格式化 String prefix = sdf.format(now); //把随机数、时间和文件名拼接作为复制后的文件名 String fileName = prefix + "_" + no + "_" + fileFileName; //生成目的地址 File destFile = new File("d:\"+fileName); //由源地址复制到目的地址 FileUtils.copyFile(file, destFile); return SUCCESS; } }
struts2.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="maya.controller.{1}Action" method="{2}"> <result> {1}/{2}.jsp </result> </action> </package> </struts>
运行:
选取文件,上传
查看D盘路径,文件已上传成功
实例二:批量上传文件
1.做界面
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h1>多文件上传</h1> <form action="UploadMany_upload" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"><br> 附件一:<input type="file" name="file"><br> 附件二:<input type="file" name="file"><br> 附件三:<input type="file" name="file"><br> <input type="submit" value="提交"> </form> </body> </html>
upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <h3>上传成功</h3> </body> </html>
2.做Action
package maya.controller; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class UploadManyAction extends ActionSupport{ private String title; private File[] file;//数组形式存储批量文件 private String[] fileFileName; private String[] fileContentType; public String input() { return SUCCESS; } public String upload() throws IOException { for (int i=0;i<file.length;i++) { File item = file[i]; if (item != null) { File temp = new File("d:\"+fileFileName[i]); FileUtils.copyFile(item, temp); } } return SUCCESS; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 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; } }
struts2.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <!-- 设置文件上传(大小)允许的最大值 ,单位Byte--> <constant name="struts.multipart.maxSize" value="20971520"/> <package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="maya.controller.{1}Action" method="{2}"> <result> {1}/{2}.jsp </result> </action> </package> </struts>
运行:
查看上传的路径: