6. Struts2 文件上传

文件上传


代码

提交页面

<%@ taglib uri="/struts-tags" prefix="s"%>
.....
<s:form action="Pic_upload" enctype="multipart/form-data" method="post">
        <s:file name="pb.pic" />
        <s:submit value="ok"/>
    </s:form>

实体类

package com.bean;

import java.io.File;

public class PicBean {
    // 文件本身
    private File pic;
    // 文件类型
    private String picContentType;
    // 文件名称
    private String picFileName;

    public PicBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    public File getPic() {
        return pic;
    }

    public void setPic(File pic) {
        this.pic = pic;
    }

    public String getPicContentType() {
        return picContentType;
    }

    public void setPicContentType(String picContentType) {
        this.picContentType = picContentType;
    }

    public String getPicFileName() {
        return picFileName;
    }

    public void setPicFileName(String picFileName) {
        this.picFileName = picFileName;
    }

}

Action类

package com.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

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

import com.bean.PicBean;

public class PicAction {
    // 文件对象
    private PicBean pb;
    // 返回路径
    private String path;

    public PicBean getPb() {
        return pb;
    }

    public void setPb(PicBean pb) {
        this.pb = pb;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String upload() {
        System.out.println("文件名称" + pb.getPicFileName());
        System.out.println("文件类型" + pb.getPicContentType());
        // 获取保存路径
        String saveFilePathString = ServletActionContext.getServletContext().getRealPath("/") + "/image/";
        // 文件目录
        File saveFile = new File(saveFilePathString);
        // 判断目录是否存在
        if (!saveFile.exists()) {
            // 不存在时 创建目录
            saveFile.mkdir();
        }
        String savePicString = saveFilePathString + pb.getPicFileName();
        File savePic = new File(savePicString);
        try {
            FileUtils.copyFile(pb.getPic(), savePic);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute("image",
                ServletActionContext.getRequest().getContextPath() + "/image/" + pb.getPicFileName());
        path = "show.jsp";
        return "ok";
    }
}

struts.xml设置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <!-- 设置该应用的字符集 -->
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <!-- 设置主题格式为简单的 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    <package name="upload" extends="struts-default" namespace="/">
    <!-- 配置上传的Action -->
        <action name="Pic_upload" class="com.action.PicAction" method="upload">
            <!-- 配置上传拦截器 -->
            <interceptor-ref name="fileUpload">
                <!-- 配置允许上传的类型 -->
                <param name="allowedTypes">image/png,image/jpg,image/jpeg,image/gif</param>
                <!-- 配置允许上传的文件最大值  单位B -->
                <param name="maximumSize">2000000</param>
            </interceptor-ref>
            <!-- 配置系统默认拦截器 -->
            <interceptor-ref name="defaultStack"/>

            <!-- 配置结果 -->
            <result name="ok">show.jsp</result>
        </action> 
    </package>
</struts>  

展示页面

展示:

<img src="${image}"><br>

实际操作

提交

提交页面

展示

展示

猜你喜欢

转载自blog.csdn.net/luke199257/article/details/80820570