`

struts上传示例

 
阅读更多
Struts2上传文件示例  2009-04-13 17:35:44|  分类: Struts2(WebWork) |  标签: |字号大中小 订阅 .

Struts2上传文件示例

源代码:

1.包如下:请自行下载


2.Action类


package com.sterning;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class StrutsFileUpload extends ActionSupport implements
        ServletContextAware {

    private File upload;// 实际上传文件

    private String uploadContentType; // 文件的内容类型

    private String uploadFileName; // 上传文件名

    private String fileCaption;// 上传文件时的备注

    private ServletContext context;

    public String execute() throws Exception {

        try {
           
            String targetDirectory = context.getRealPath("/upload");
            String targetFileName = uploadFileName;
            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);           
           
            setUploadFileName(target.getPath());//保存文件的存放路径
        } catch (Exception e) {

            addActionError(e.getMessage());

            return INPUT;
        }

        return SUCCESS;

    }

    public String getFileCaption() {
        return fileCaption;
    }

    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setServletContext(ServletContext context) {
        this.context = context;
    }

}

3.页面

上传页面:upload.jsp

<%@ page language="java" contentType="text/html; charset=GB2312"%>  
<%@ taglib prefix="s" uri="/struts-tags" %>  
<html>
    <head>
        <title>文件上传示例</title>
        <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
            type="text/css" />

    </head>

    <body>

        <s:actionerror />
        <s:fielderror />
        <s:form action="doUpload" method="POST" enctype="multipart/form-data">
            <tr>
                <td colspan="2">
                    <h1>
                        文件上传示例
                    </h1>
                </td>
            </tr>

            <s:file name="upload" label="上传的文件" />
            <s:textfield name="fileCaption" label="备注" />
            <s:submit value="上   传"/>
        </s:form>
    </body>
</html>

上传成功页面:upload_success.jsp

<%@ page language="java" contentType="text/html; charset=GB2312"%> 
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>上传成功</title>
        <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
            type="text/css" />
    </head>

    <body>
        <table class="wwFormTable">
            <tr>

                <td colspan="2">
                    <h1>
                        上传成功
                    </h1>
                </td>
            </tr>

            <tr>
                <td class="tdLabel">
                    <label for="doUpload_upload" class="label">
                        内容类型:
                    </label>
                </td>
                <td>
                    <s:property value="uploadContentType" />
                </td>
            </tr>

            <tr>
                <td class="tdLabel">
                    <label for="doUpload_upload" class="label">
                        文件路径:
                    </label>
                </td>
                <td>
                    <s:property value="uploadFileName" />
                </td>
            </tr>


            <tr>
                <td class="tdLabel">
                    <label for="doUpload_upload" class="label">
                        临时文件:
                    </label>
                </td>
                <td>
                    <s:property value="upload" />
                </td>
            </tr>

            <tr>
                <td class="tdLabel">
                    <label for="doUpload_upload" class="label">
                        备注:
                    </label>
                </td>
                <td>
                    <s:property value="fileCaption" />
                </td>
            </tr>


        </table>

    </body>
</html>

4.struts.xml

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

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="GB2312" />

    <package name="NG" namespace="/" extends="struts-default">
        <action name="showUpload">
            <result>/upload.jsp</result>
        </action>
       
        <action name="doUpload" class="com.sterning.StrutsFileUpload">
            <result name="input">/upload.jsp</result>
            <result>/upload_success.jsp</result>
        </action>
    </package>

</struts>


5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>customization</display-name>

    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter> 


    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>


    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

  • 大小: 5.4 KB
分享到:
评论

相关推荐

    struts文件上传示例

    在这个"Struts文件上传示例"中,我们将深入探讨如何在Struts框架下实现文件上传功能,以及相关的关键知识点。 首先,理解文件上传的基本流程至关重要。在Web应用中,用户通过表单选择本地文件,然后提交到服务器。...

    完整struts2文件上传示例

    以下是对"完整Struts2文件上传示例"的详细解释: 1. **配置Struts2框架** 在Struts2中,我们首先需要在`struts.xml`配置文件中添加相关的拦截器(interceptor)来处理文件上传。`struts.multipart.parser`属性应...

    struts2 上传 示例

    这个"struts2 上传 示例"是一个用于演示如何在Struts2框架下实现文件上传的示例项目。 首先,我们要了解Struts2文件上传的基本原理。Struts2使用`Commons FileUpload`库处理文件上传,该库是Apache Commons项目的一...

    Struts2 上传图片示例

    以上就是一个基础的Struts2图片上传示例。通过这个例子,你可以了解Struts2如何与Apache Commons FileUpload库协作来处理文件上传,以及如何在Action类中处理上传后的逻辑。在实际项目中,你可能需要根据具体需求...

    struts2文件、图片上传示例

    在Struts2中,实现文件和图片上传相对直观,这主要得益于其强大的Action类和拦截器机制。 首先,我们需要在Struts2配置文件(struts.xml)中定义一个处理文件上传的Action。这个Action通常会有一个或多个成员变量,...

    Struts2文件上传示例

    这个"Struts2文件上传示例"包含了如何在实际项目中实现这一功能的代码和配置。 首先,我们需要了解Struts2中的Action类。Action类是处理用户请求的核心,通常会有一个或多个方法与JSP页面上的表单提交事件关联。在...

    Struts2文件上传程序示例

    这个示例程序旨在帮助初学者理解如何在Struts2框架下实现文件上传。 首先,我们需要了解Struts2文件上传的核心概念和组件。在Struts2中,文件上传主要依赖于Apache Commons FileUpload库。这个库提供了处理HTTP多...

    struts上传功能代码示例

    下面我们将详细探讨如何在Struts框架中实现文件上传,并通过代码示例进行解析。 首先,我们需要在Struts配置文件(struts-config.xml)中定义一个Action,这个Action将处理文件上传请求。例如: ```xml ``` ...

    struts2.0 文件上传示例

    在本示例中,我们将深入探讨如何利用Struts2.0框架实现图片上传,并关注如何限制文件类型、大小以及保存路径。 首先,我们需要了解Struts2.0的核心概念——Action类。Action类是处理用户请求的核心,它通常继承自`...

    struts实现的文件上传下载功能

    在这个特定的场景中,我们关注的是如何使用Struts来实现文件的上传和下载功能。这个功能对于任何Web应用来说都是非常重要的,因为它允许用户交互地处理数据和资源。 首先,我们需要理解文件上传的基本流程。在...

    JavaEE Struts文件上传

    7. **Day01_StrutsUpload**:这个文件夹可能包含了一个示例项目,其中包括了Struts2配置文件、Action类、HTML表单以及相关的资源文件。通过分析这些文件,你可以看到一个完整的Struts2文件上传应用的结构和实现细节...

    struts 图片上传demo

    这个"struts 图片上传demo"是Struts框架中实现图片上传功能的一个示例项目,它包含了对文件大小和类型的限制,并且代码注释详尽,方便理解。 在Web开发中,文件上传是一项常见的需求,特别是处理用户上传图片的情况...

    struts2文件上传下载源代码

    在Struts2中,文件上传和下载是常见的功能需求,特别是在处理用户交互和数据交换时。这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件...

    一个Struts1多文件上传实例(附Form中传List示例)

    本实例主要探讨如何在Struts1中实现多文件上传功能,并结合Form中传递List类型的数据,这对于理解MVC模式下的文件处理和数据传递有重要作用。我们将深入讨论以下几个关键知识点: 1. **Struts1框架基础**: Struts...

    struts1上传下载示例

    ### Struts1 上传下载示例详解 #### 一、简介 本文档旨在详细介绍如何使用Struts1框架完成文件的上传与下载功能,并且能够支持远程操作。这为那些需要在网络环境中交换文件的应用程序提供了非常实用的功能。 #### ...

    struts上传(已解决中文问题)

    在Struts框架中处理文件上传功能是一项常见的任务,但中文文件名在上传过程中可能会遇到乱码问题。这个问题通常与字符编码设置、服务器配置以及Struts的配置有关。 首先,让我们深入了解一下文件上传的基本原理。在...

    Struts1.x的上传文件示例

    本示例将深入探讨如何在Struts1.x中实现文件上传功能。 首先,你需要在Struts的配置文件(struts-config.xml)中定义一个Action,这个Action将会处理文件上传请求。你可能需要创建一个新的Action类,继承自Struts的...

    Jquery+struts2上传图片,制作进度条,以及Jquery+Json+Struts交互等示例

    Jquery+struts2上传图片,制作进度条等示例,还有Jquery+Json+Struts实现Ajax技术,还有图形处理技术等,虽然很少,但都有重点突出,由于我也是学习,所以代码不是很简洁,对想学习交流的朋友非常合适

    Struts上传下载范例

    首先,我们来看`fileupload.rar`,这个文件可能包含了一个简单的Struts文件上传的示例。在Struts中,文件上传通常依赖于`Apache Commons FileUpload`库。开发者需要配置Struts的ActionForm类来接收上传的文件,并在...

Global site tag (gtag.js) - Google Analytics