Java框架Struts2实现图片上传

来源:互联网 发布:如家精选 知乎 编辑:程序博客网 时间:2024/06/05 15:16

Struts 2 框架为处理文件上传提供了内置支持,它使用“在 HTML 中基于表单的文件上传”。当上传一个文件时,它通常会被存储在一个临时目录中,而且它们应该由 Action 类进行处理或移动到一个永久的目录,用来确保数据不丢失。服务器在恰当的位置可能有一个安全策略,它会禁止你写到除了临时目录以外的目录,而且这个目录属于你的web应用应用程序。

通过预定义的名为文件上传的拦截器,Struts 的文件上传是可能的,这个拦截器在 org.apache.struts2.interceptor.FileUploadInterceptor 类是可用的,而且是 defaultStack 的一部分。

创建视图文件

让我们开始创建需要浏览和上传选定的文件的视图。因此,让我们创建一个带有简单的 HTML 上传表单的 index.jsp,它允许用户上传文件:(表单的编码类型设置为multipart/form-data

<%--  Created by IntelliJ IDEA.  User: yzjxiaoyue  Date: 2017/7/28  Time: 19:11  To change this template use File | Settings | File Templates.--%><%@ page language="java" contentType="text/html; charset=utf-8"         pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head>    <title>File Upload</title></head><body><form action="upload" method="post" enctype="multipart/form-data">    <label for="myFile">Upload your file</label>    <input type="file" name="myFile"  id="myFile"/>    <input type="submit" value="Upload"/></form></body></html>
之后创建success.jsp页面:

<%--  Created by IntelliJ IDEA.  User: yzjxiaoyue  Date: 2017/7/28  Time: 19:14  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head>    <title>File Upload Success</title></head><body>You have successfully uploaded <s:property value="myFileFileName"/></body></html>

创建error.jsp页面

<%--  Created by IntelliJ IDEA.  User: yzjxiaoyue  Date: 2017/7/28  Time: 20:05  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html; charset=UTF-8" %><%@ taglib prefix="s" uri="/struts-tags" %><html><head>    <title>File Upload Error</title></head><body>There has been an error in uploading the file.</body></html>

创建 action 类

接下来让我们创建一个称为 uploadFile.java 的 Java 类,它负责上传文件,并且把这个文件存储在一个安全的位置:

package com.action;import com.opensymphony.xwork2.ActionSupport;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;public class uploadFile extends ActionSupport{    private File myFile;    public File getMyFile() {        return myFile;    }    public void setMyFile(File myFile) {        this.myFile = myFile;    }    private String myFileContentType;    private String myFileFileName;    private String destPath;    public String execute()    {         /* Copy file to a safe location */        destPath = "E:\\Program Files\\apache-tomcat-9.0.0\\apache-tomcat-9.0.0.M22\\work\\";        try{            System.out.println("Src File name: " + myFile);            System.out.println("Dst File name: " + myFileFileName);            File destFile  = new File(destPath, myFileFileName);            FileUtils.copyFile(myFile, destFile);        }catch(IOException e){            e.printStackTrace();            return ERROR;        }        return SUCCESS;    }    public String getMyFileContentType() {        return myFileContentType;    }    public void setMyFileContentType(String myFileContentType) {        this.myFileContentType = myFileContentType;    }    public String getMyFileFileName() {        return myFileFileName;    }    public void setMyFileFileName(String myFileFileName) {        this.myFileFileName = myFileFileName;    }}

配置文件

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"        "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <constant name="struts.devMode" value="true"/>    <constant name="struts.multipart.maxSize" value="10000000"/>    <constant name="struts.multipart.saveDir" value="/tmp"/>    <constant name="struts.custom.i18n.resources" value="struts"></constant>    <package name="default" namespace="/" extends="struts-default">        <action name="upload" class="com.action.uploadFile">            <!--<interceptor-ref name="basicStack"/>-->            <interceptor-ref name="defaultStack"/>            <interceptor-ref name="fileUpload">                <param name="allowedTypes">image/jpeg,image/jpg,image/gif</param>            </interceptor-ref>            <result name="success">/success.jsp</result>            <result name="error">/error.jsp</result>        </action>    </package></struts>

界面截图








原创粉丝点击