struts2上传文件

来源:互联网 发布:java中setname方法 编辑:程序博客网 时间:2024/06/06 00:03


1、struts2的核心配置文件:

    <!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。        如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->    <!-- <constant name="struts.action.extension" value="do" /> -->        <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->    <constant name="struts.serve.static.browserCache" value="false" />        <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->    <constant name="struts.configuration.xml.reload" value="true" />        <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->    <constant name="struts.devMode" value="true" />        <!-- 默认的视图主题 -->    <constant name="struts.ui.theme" value="simple" />    <!-- 让spring来管理struts的action bean --><constant name="struts.objectFactory" value="spring"/>    <!--解决乱码    -->    <constant name="struts.i18n.encoding" value="UTF-8" />        <!-- 最大5M -> 1024KB=1MB 1024byte=1KB 50M=1024×1024×5×10 -->    <constant name="struts.multipart.maxSize" value="52428800"/>



2、使用servlet特性

struts2 action接受文件

    private File[] image; //上传的文件    private String[] imageFileName; //文件名称    private String[] imageContentType; //文件类型        public String upload1() throws Exception {        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");        String realpath = ServletActionContext.getServletContext().getRealPath("/upload");        System.out.println(realpath);        if (image != null) {            File savedir=new File(realpath);            if(!savedir.getParentFile().exists())                savedir.getParentFile().mkdirs();            for(int i=0;i<image.length;i++){                File savefile = new File(savedir, imageFileName[i]);                FileUtils.copyFile(image[i], savefile);                System.out.println(savefile);            }            ActionContext.getContext().put("message", "文件上传成功");        }        return "success";    }

struts2配置文件

        <action name="upload1" class="productAction" method="upload1">            <result name="success">/index.jsp</result>        </action>

JSP页面:

<form accept-charset="UTF-8" action="<%=ctxPath %>/product/upload1" enctype="multipart/form-data"  method="post" >文件1<input type="file"   name="image"  /><p/>文件2<input type="file"   name="image"  /></form>


2、和第一种是类似的,只不过在struts配置文件中加上了路径,不过个人认为还不如放到配置文件里面了

java代码:

    private File[] image; //上传的文件    private String[] imageFileName; //文件名称    private String[] imageContentType; //文件类型        /**     * 返回上传文件保存的位置     */    private String savePath;    public String upload2() throws Exception {    savePath = ServletActionContext.getServletContext().getRealPath(savePath);        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");        //取得需要上传的文件数组        File[] files = getImage();        if (files !=null && files.length > 0) {            for (int i = 0; i < files.length; i++) {                //建立上传文件的输出流, getImageFileName()[i]                FileOutputStream fos = new FileOutputStream(savePath + "\\" + getImageFileName()[i]);                //建立上传文件的输入流                FileInputStream fis = new FileInputStream(files[i]);                byte[] buffer = new byte[1024];                int len = 0;                while ((len=fis.read(buffer))>0) {                    fos.write(buffer, 0, len);                }                fos.close();                fis.close();            }        }        return SUCCESS;    }

struts配置文件

        <action name="upload2" class="productAction" method="upload2">            <!-- 要创建/upload文件夹,否则会报找不到文件 -->            <param name="savePath">/upload</param>            <result name="success">/index.jsp</result>        </action>


JSP文件:

<form accept-charset="UTF-8" action="<%=ctxPath %>/product/upload2" enctype="multipart/form-data"  method="post" >文件1<input type="file"   name="image"  /><p/>文件2<input type="file"   name="image"  /></form>

4、List形式上传文件,这个方式其实和原来的数组的其实差不多

java代码:

    private List<File> image; // 上传的文件    private List<String> imageFileName; // 文件名称    private List<String> imageContentType; // 文件类型    private String savePath;    public String upload() throws Exception {        ServletActionContext.getRequest().setCharacterEncoding("UTF-8");        savePath = ServletActionContext.getServletContext().getRealPath(savePath);        // 取得需要上传的文件数组               List<File> files = image;        if (files != null && files.size() > 0) {            for (int i = 0; i < files.size(); i++) {                FileOutputStream fos = new FileOutputStream(savePath + "\\" + imageFileName.get(i));                FileInputStream fis = new FileInputStream(files.get(i));                byte[] buffer = new byte[1024];                int len = 0;                while ((len = fis.read(buffer)) > 0) {                    fos.write(buffer, 0, len);                }                fis.close();                fos.close();            }        }        return SUCCESS;    }

struts2配置:

        <action name="upload" class="productAction" method="upload">            <!-- 要创建/image文件夹,否则会报找不到文件 -->            <param name="savePath">/upload</param>            <result name="success">/index.jsp</result>        </action>

JSP:

<form accept-charset="UTF-8" action="<%=ctxPath %>/product/upload" enctype="multipart/form-data"  method="post" >文件1<input type="file"   name="image"  /><p/>文件2<input type="file"   name="image"  /></form>




0 0
原创粉丝点击