`
wengsibo
  • 浏览: 82571 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

struts2 文件上传

阅读更多
1.导入struts的包
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.1.jar

2.建立jsp页面
2.1 先将表单的提交方式设为POST,然后将enctype设为multipart/form-data
2.2 <input type="file" name="paperFile" size="30" /> 例如在页面上有个name定义为paperFile的文件

3.在Action里写以下代码
3.1 定义相关属性
	private static final int BUFFER_SIZE = 16 * 1024 ;
				private File paperFile;  //对应jsp页面上的name
    	  private String contentType; //上传文件的MIME类型,必参
    	  private String fileName;   //文件名

     
    3.2 生成相应的set方法
   
   
/**
    			*必须方法
				 * 把页面上的文件类型传到action,
				 * 方法写法 set{jsp页面定义的file的name,首字母大写}ContentType(String contentType)
				 * {this .contentType = contentType;}
				 * @param contentType
				 */
    			public void setPaperFileContentType(String contentType)  {
			        this.contentType = contentType; //这里的this.contentType 就是上面定义的属性
			    } 
   				
   				/**
				 * 必须方法
				 * 把页面上的文件名传到action,
				 * 方法写法 set{jsp页面定义的file的name,首字母大写}FileName(String fileName)
				 * {this .fileName = fileName;}
				 * @param fileName
				 */
		   	  public void setPaperFileFileName(String fileName)  {
			        this.fileName = fileName; //这里的this.fileName 就是上面定义的属性
			    }
			    /**
				 * 必须方法,
				 * 把文件传到action
				 * @param paperFile
				 */
			    public void setPaperFile(File paperFile) {
						this.paperFile = paperFile;
					}
					

4. 要struts 配置中加入以下内容

<action name="fileUpload" class="com.doone.servant.exam.action.PaperAction" method="UploadPaper">
			<interceptor-ref name ="fileUploadStack" />  <!--这是必须加的,因为Struts使用fileUpload拦截器将文件绑定到Action的实例-->
			<result>/exam/exam_manage/paper_list.jsp</result>
		</action>
	


5. 在工程的web.xml上配置
<filter>
					<filter-name>struts-cleanup</filter-name>
					<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
			</filter>
			<filter-mapping>
					<filter-name>struts-cleanup</filter-name>
					<url-pattern>/*</url-pattern>
			</filter-mapping>
			

6. 上面的步骤只是把文件上传到一个临时文件夹下.下面我们要做的是从临时文件夹下把这个文件拷贝到自己定义的文件夹下
使用 工具里面的 FileUtils.copyFile(原文件,新的文件夹); 方法来实现.
分享到:
评论

相关推荐

    struts2文件上传下载源代码

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

    struts2文件上传jar

    这个压缩包包含了实现Struts2文件上传所需的全部jar包,这些库文件对于理解和实现文件上传功能至关重要。 首先,我们要了解Struts2文件上传的基本流程。当用户通过表单提交包含文件输入字段的请求时,Struts2框架会...

    简单易懂的struts2文件上传

    在Struts2中,文件上传功能是一个常见的需求,例如用户可能需要上传图片、文档或其他类型的文件。本教程将深入浅出地讲解如何在Struts2中实现文件上传,并提供一个简单的实例来帮助理解。 1. **Struts2文件上传概述...

    struts2文件上传

    Struts2 文件上传是Web开发中的一个重要功能,它允许用户从他们的本地计算机向服务器传输文件。在Struts2框架中,文件上传是通过特定的拦截器实现的,这些拦截器处理了文件上传请求并提供了安全性和大小限制。下面将...

    Struts2文件上传

    Struts2文件上传是Web开发中常见的功能,用于允许用户在网页上选择并上传本地文件到服务器。在Java Web环境中,Struts2框架提供了一套完整的解决方案来处理文件上传请求。下面将详细介绍Struts2文件上传的核心概念、...

    struts 2文件上传

    在Struts 2中,文件上传功能是通过使用Struts 2的插件机制来实现的,这使得开发者能够方便地处理用户上传的文件。下面将详细讨论Struts 2文件上传的相关知识点。 ### 1. Struts 2文件上传原理 文件上传是基于HTTP...

    Struts2文件上传带进度条页面无刷新

    "Struts2文件上传带进度条页面无刷新"的实现涉及多个技术点,包括Struts2的文件上传插件、AJAX异步通信以及前端进度条展示。 首先,Struts2的文件上传依赖于`struts2-upload-plugin`。这个插件扩展了Struts2的核心...

    Struts2之struts2文件上传详解案例struts011

    在这个"Struts2之struts2文件上传详解案例struts011"中,我们将深入探讨如何实现这一功能。 首先,我们需要了解Struts2中的Action类,它是处理用户请求的核心组件。为了支持文件上传,我们需要创建一个继承自`org....

Global site tag (gtag.js) - Google Analytics