`
only1
  • 浏览: 21688 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

struts2文件上传

阅读更多

由于当时写Demo时,是在原来的HelloWorld 上改,所以类名没有改过来。

Action类的代码主如下

/**
 * 
 */
package com.demo.view.action;

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

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

/**
 * @author Administrator
 *
 */
public class HelloWorld {
	
	private File image;
	private String imageFileName;
	private String imageContentType;
	

	public File getImage() {
		return image;
	}


	public void setImage(File image) {
		this.image = image;
	}


	public String getImageFileName() {
		return imageFileName;
	}


	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}


	public String getImageContentType() {
		return imageContentType;
	}


	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	
	public String getHello(){
		return "success";
	}


	public String upload() throws IOException{
		String realPath=ServletActionContext.getServletContext().getRealPath("/images");
		System.out.println(realPath);
		if(null!=image){
			File saveFile=new File(new File(realPath),imageFileName);
			if(!saveFile.getParentFile().exists()){
				saveFile.getParentFile().mkdirs();
			}
			FileUtils.copyFile(image,saveFile);
			ServletActionContext.getRequest().setAttribute("message", "文件上传成功");
			
		}
		
		return "success";
	}

}

  

 

   struts.xml配置文件

 

<struts>
	<package name="myDemo"  namespace="/demo"  extends="struts-default">
		<action name="helloWorld_*" class="com.demo.view.action.HelloWorld" method="{1}">
			<result name="success">
				/WEB-INF/pages/hello.jsp
			</result>
		</action>
	</package>
</struts>

 

  jsp页面

 

  <body>
   <form action="helloWorld_upload" method="post" enctype="multipart/form-data">
   	文件:<input type="file" name="image"><br>
   	<input type="submit" value="上传">
   </form>
   ${message }
  </body>

 

 

 

分享到:
评论

相关推荐

    struts2文件上传下载源代码

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

    struts2 文件上传

    Struts2 文件上传是Web开发中的一个重要功能,它允许用户通过网页上传文件到服务器。Struts2 是一个基于MVC(Model-View-Controller)设计模式的Java Web框架,提供了丰富的特性和强大的控制层功能,使得文件上传...

    struts2文件上传jar

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

    简单易懂的struts2文件上传

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