Struts2_文件上传

人有一个特点,越是得不到的东西,越想得到,越是不能接触的东西就越想接触。反过来,越容易得到的东西越不知珍惜。

文件上传的方法很简单,注意几个点:

1.在前端界面,表单中加上enctype="multipart/form-data"
2.在execute()方法中,要加上FileUtils.copyFile(photo[j], destFile);,因为表单上传后会作为一个临时文件存储,所以要用前面的代码将临时文件复制到目标文件夹中。

1.单文件上传

前段页面代码:

<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8" %>

<!DOCTYPE html >
<html>
<head>
<meta  charset="UTF-8">
<title>上传界面</title>
</head>
<body>
<form action="login.action" method="post" enctype="multipart/form-data">
    姓名:<input type="text" name="username"><br>
    图片:<input type="file" name="photo"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

struts代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

    <struts>
    <!-- 声明常量:关联自己写的proporties文件 -->
        <constant name="struts.custom.i18n.resources" value="message"/>
    <!-- 声明常量:配置文件上传的最大尺寸 -->
        <constant name="struts.multipart.maxSize" value="5242880"/>
        <package name="root" namespace="/" extends="struts-default">
            <action name="login" class="com.action.LoginAction">
           <!-- 重写fileUpload拦截器 -->
                <interceptor-ref name="fileUpload">
                <!-- 允许上传的文件格式 -->
                    <param name="allowedTypes">
                        image/bmp,image/png,image/gif,image/pjpeg,image/jpg 
                    </param>
                <!-- 允许上传的文件最大尺寸 -->
                    <param name="maximumSize">2097152</param>
                </interceptor-ref>
                <!-- 注意:一定加上默认的拦截器,当引用了自己写的拦截器后默认的就不生效了 -->
                <interceptor-ref name="defaultStack"/>
                <result>/success.jsp</result>
            </action>
        </package>
    </struts>

Action代码:

package com.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class LoginAction {
    private String username;
    private File photo;
    private String photoFileName;
    private String photoContentType;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public File getPhoto() {
        return photo;
    }
    public void setPhoto(File photo) {
        this.photo = photo;
    }
    public String getPhotoFileName() {
        return photoFileName;
    }
    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }
    public String getPhotoContentType() {
        return photoContentType;
    }
    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String execute() throws Exception{
        return "success";
    }
}

完成以上功能,只是将文件上传到一个临时的目录中,如果要将文件保存到指定目录,需要对临时目录中的文件进行操作,往往需要对临时目录的文件进行复制操作。

public String execute() throws Exception{
    File destFile=new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName));
    FileUtils.copyFile(photo, destFile);
    return "success";
    }

2.多文件上传

上传多个文件,只需将上例中与文件相关的属性改成数组格式,将上传的方法也改成操作数组即可。

Action代码:

package com.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

public class LoginAction {
    private String username;
    private File[] photo;
    private String[] photoFileName;
    private String[] photoContentType;


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public File[] getPhoto() {
        return photo;
    }


    public void setPhoto(File[] photo) {
        this.photo = photo;
    }


    public String[] getPhotoFileName() {
        return photoFileName;
    }


    public void setPhotoFileName(String[] photoFileName) {
        this.photoFileName = photoFileName;
    }


    public String[] getPhotoContentType() {
        return photoContentType;
    }


    public void setPhotoContentType(String[] photoContentType) {
        this.photoContentType = photoContentType;
    }


    public String execute() throws Exception{
        for (int j = 0; j < photo.length; j++) {
            System.out.println(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName));
            File destFile=new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName[j]));
            FileUtils.copyFile(photo[j], destFile);

        }
        return "success";

    }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struts2_(016_017)_bug_repair是指修复struts2框架版本16和17中的漏洞问题。Struts2是一个开源的Java Web应用程序框架,被广泛使用于构建企业级Web应用程序。然而,在早期版本的struts2中存在一些安全漏洞,这些漏洞可能会被黑客利用,造成系统受到攻击和数据泄露等问题。 为了解决这些安全漏洞问题,struts2的开发团队发布了struts2_(016_017)_bug_repair。这个修复程序主要包括了对未经验证的用户输入数据的处理改进,以及对文件上传功能的安全加强等方面的修复。通过应用该修复程序,用户可以提高系统的安全性,降低被攻击的风险。 下载struts2_(016_017)_bug_repair的步骤如下: 1. 打开struts2官方网站或其他可信的软件下载网站。 2. 在搜索栏中输入"struts2_(016_017)_bug_repair"并点击搜索按钮。 3. 在搜索结果中选择可信度较高的下载链接,并点击下载按钮。 4. 等待下载完成,将修复程序保存到本地计算机的合适位置。 一旦下载完成,就可以根据修复程序的安装指南进行安装和配置。根据不同的操作系统和开发环境,安装过程可能会有所不同。建议在安装之前备份现有的struts2框架及应用程序代码,以防止不可预测的问题发生。 总之,通过下载和应用struts2_(016_017)_bug_repair,可以提高系统的安全性,并降低系统遭受安全漏洞攻击的风险。同时,及时关注和应用官方发布的安全补丁和更新是保护Web应用程序安全的重要措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值