struts2上传文件


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
疫情居家办公系统管理系统按照操作主体分为管理员和用户。管理员的功能包括办公设备管理、部门信息管理、字典管理、公告信息管理、请假信息管理、签到信息管理、留言管理、外出报备管理、薪资管理、用户管理、公司资料管理、管理员管理。用户的功能等。该系统采用了MySQL数据库,Java语言,Spring Boot框架等技术进行编程实现。 疫情居家办公系统管理系统可以提高疫情居家办公系统信息管理问题的解决效率,优化疫情居家办公系统信息处理流程,保证疫情居家办公系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理公告,管理疫情居家办公系统信息,包括外出报备管理,培训管理,签到管理,薪资管理等,可以管理公告。 外出报备管理界面,管理员在外出报备管理界面中可以对界面中显示,可以对外出报备信息的外出报备状态进行查看,可以添加新的外出报备信息等。签到管理界面,管理员在签到管理界面中查看签到种类信息,签到描述信息,新增签到信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值