Struts2上传单文件或者多个文件的好处
问题?Struts2上传单文件或者多个文件的好处在哪里呢?我们都知道上传文件的基本步骤都是读取文件域,得到文件名,然后得到真实存储路径,然后再构建输入输出流,这样几个步骤,我们的文件上传也就搭成了。显得有些麻烦了是吧?接下来让我们去看看,struts2给我们带来的便利。
原理:struts2拥有自动转换类型的功能,这是好处一。struts2我们所拷入的common-io.jar包集成了一个工具类,运用这个工具类可以帮我们达到上传文件的目的。
*首先搭建好struts2的环境(7个jar包,拦截器之类的配置),详情可看:http://blog.csdn.net/mr_li13/article/details/49391329?
案例:运用struts2肯定要用它的配置文件struts.xml啦、
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration <span style="color:#ff0000;">2.0</span>//EN"
- "http://struts.apache.org/dtds/struts-<span style="color:#ff0000;">2.0</span>.dtd">
- <struts>
- <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 各个配置文件的包含 -->
- <!-- 单个文件下载action -->
- <include file="UpLoadFile.xml"></include>
- <!-- 多个文件上传action -->
- <include file="moreUpLoadFile.xml"></include>
- </struts>
红色标记处。一定要相同。不然就会出不联网就会报错。详情请看
1.单个文件:
UpLoadFile.xml文件内容
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 设置文件上传大小 50M-->
- <constant name="struts.multipart.maxSize" value="52428800"></constant>
- <!-- 文件下载action -->
- <package name="upload" namespace="/upload" extends="struts-default">
- <action name="upload1" class="com.itcast.web.domain.uploadAction" method="execute">
- <result name="success">/uploadSuccess.jsp</result>
- </action>
- </package>
- </struts>
上传页面:upload.jsp
[html] view plain copy print ?
- <!-- 于http://localhost:8080/Struts2day1/upload.jsp -->
- <form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
- 文件:<input type="file" name="img"/><br/><!-- name这个名字必须和action类中的setter名字相同,首先执行setter方法,struts2会自动封装 -->
- <input type="submit" value="上传"/>
- </form>
action实体类:uploadAction.class
[java] view plain copy print ?
- package com.itcast.web.domain;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class uploadAction extends ActionSupport implements Serializable {
- private File img;//代表表单中文件上传输入域的名称,struts会自动封装成file域在此进行处理
- private String imgFileName;
- //得到上传域中,文件的名字,是不是很方便啊,struts已经为我们做好了
- //只需要与File的对象名称多FileName就可以了,而且文件名的编码格式已经转化好了
- private String imgContentType;//同理封装好了一个文件类型类
- public File getImg() {
- return img;
- }
- public void setImg(File img) {
- this.img = img;
- }
- public String getImgFileName() {
- return imgFileName;
- }
- public void setImgFileName(String imgFileName) {
- this.imgFileName = imgFileName;
- }
- public String getImgContentType() {
- return imgContentType;
- }
- public void setImgContentType(String imgContentType) {
- this.imgContentType = imgContentType;
- }
- public String execute(){
- // 我们自然用最新的方式。简便许多
- try {
- System.out.println(imgContentType);//image/jpeg
- //找到文件存放路径
- ServletContext sc=ServletActionContext.getServletContext();
- String storpath=sc.getRealPath("files");
- //老方式
- //构建输出流
- // OutputStream out=new FileOutputStream(storpath+"\\"+imgFileName);
- // InputStream in=new FileInputStream(img);
- //
- // byte b[]=new byte[1024];
- // int len=-1;
- // while((len=in.read(b))!=-1){
- // out.write(b, 0, len);
- // }
- // out.close();
- // in.close();
- //新方式,用拷入的jar包commons-io这个jar包,
- //里面有封装的一个类,使我们上传文件更容易(前者是文件源,后者是路径(主路径,子路径))
- FileUtils.copyFile(img, new File(storpath, imgFileName));
- ActionContext.getContext().put("message", "上传成功!");
- return SUCCESS;
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
- }
成功页面:uploadSuccess.jsp
[html] view plain copy print ?
- <body>
- ${message }
- </body>
2.多文件上传
其实方式都差不多只是将实体类中的属性变成了数组形式
moreUploadFile.xml
[html] view plain copy print ?
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- constant表示常量设置,设置配置文件自动更新,开发中很重要 -->
- <constant name="struts.action.extension" value="action,,do"></constant>
- <constant name="struts.devMode" value="true"></constant>
- <!-- 设置文件上传大小 50M-->
- <constant name="struts.multipart.maxSize" value="52428800"></constant>
- <package name="moreupload" namespace="/moreupload" extends="struts-default">
- <action name="upload1" class="com.itcast.web.domain.moreUploadAction" method="execute">
- <result name="success">/uploadSuccess.jsp</result>
- </action>
- </package>
- </struts>
moreupload.jsp
[html] view plain copy print ?
- <!-- http://localhost:8080/Struts2day1/upload.jsp -->
- <form action="${pageContext.request.contextPath}/moreupload/upload1.action" method="post" enctype="multipart/form-data">
- 文件1:<input type="file" name="img"/><br/><!-- name这个名字必须和action类中的setter名字相同,首先执行setter方法,struts2会自动封装 -->
- 文件2:<input type="file" name="img"/><br/>
- <input type="submit" value="上传"/>
- </form>
实体类:moreUploadAction.class
[java] view plain copy print ?
- package com.itcast.web.domain;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import javax.servlet.ServletContext;
- import org.apache.commons.io.FileUtils;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class moreUploadAction extends ActionSupport implements Serializable {
- private File[] img;//代表表单中文件上传输入域的名称,struts会自动封装成file域在此进行处理
- private String[] imgFileName;
- //得到上传域中,文件的名字,是不是很方便啊,struts已经为我们做好了
- //只需要与File的对象名称多FileName就可以了,而且文件名的编码格式已经转化好了
- private String[] imgContentType;//同理封装好了一个文件类型类
- public File[] getImg() {
- return img;
- }
- public void setImg(File[] img) {
- this.img = img;
- }
- public String[] getImgFileName() {
- return imgFileName;
- }
- public void setImgFileName(String[] imgFileName) {
- this.imgFileName = imgFileName;
- }
- public String[] getImgContentType() {
- return imgContentType;
- }
- public void setImgContentType(String[] imgContentType) {
- this.imgContentType = imgContentType;
- }
- public String execute(){
- try {
- if(img!=null&&img.length>0){
- ServletContext sc=ServletActionContext.getServletContext();
- String storpath=sc.getRealPath("files");
- for(int i=0;i<img.length;i++){
- FileUtils.copyFile(img[i], new File(storpath, imgFileName[i]));
- }
- }
- ActionContext.getContext().put("message", "上传成功!");
- return SUCCESS;
- } catch (Exception e) {
- e.printStackTrace();
- return ERROR;
- }
- }
- }
成功页面和uploadSuccess.jso内容一样的,共用。