01.创建对应的上传页面
<body> <form action="user/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"/> <input type="submit" value="登录"/> </form> </body>
02.创建对应的struts.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.devMode" value="true"/> <!-- 设置文件上传的大小 --> <constant name="struts.multipart.maxSize" value="20971520000"/> <package name="default" namespace="/user" extends="struts-default"> <!-- 文件上传 --> <action name="upload" class="cn.bdqn.action.UploadAction" method="upload"> <!--限制 文件上传类型 MIME 服务器中conf文件夹的web.xml --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <result name="input">/error.jsp</result> <result>/success.jsp</result> </action> </package> </struts>
03.创建对应的Action
public class UploadAction extends ActionSupport { //上传的文件 private File upload; /** * 上传文件的名称 * 当前系统的毫秒数+随机数+文件名 */ private String uploadFileName; //上传文件的类型 private String uploadContentType; public String upload(){ System.out.println("进入了 文件上传.........."); //指定文件上传的位置 String path = ServletActionContext.getServletContext().getRealPath("/upload"); //文件的名称 File.separatorChar(系统的分隔符 区分Window 和 Linux) String name=path+File.separatorChar+uploadFileName; System.out.println("文件的名称是:"+name); try { FileUtils.copyFile(upload, new File(name)); } catch (IOException e) { e.printStackTrace(); return INPUT; } return SUCCESS; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } }
04.错误界面!省略!
05.多个文件上传 界面修改
<body> <form action="user/upload" method="post" enctype="multipart/form-data"> <input type="file" name="upload"/> <input type="file" name="upload"/> <input type="file" name="upload"/> <input type="submit" value="登录"/> </form> </body>
06.Action修改后的代码
public class UploadAction extends ActionSupport { //上传的文件 private File [] upload; /** * 上传文件的名称 * 当前系统的毫秒数+随机数+文件名 */ private String [] uploadFileName; //上传文件的类型 private String [] uploadContentType; //多文件上传 public String upload(){ System.out.println("进入了 多文件上传.........."); //指定文件上传的位置 String path = ServletActionContext.getServletContext().getRealPath("/upload"); //文件的名称 File.separatorChar(系统的分隔符 区分Window 和 Linux) String name=""; for (int i = 0; i <upload.length; i++) { name=path+File.separatorChar+uploadFileName[i]; System.out.println("文件的名称是:"+name); try { FileUtils.copyFile(upload[i], new File(name)); //真正上传 } catch (IOException e) { e.printStackTrace(); return INPUT; } } return SUCCESS; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } }