struts2实现文件上传
Struts2中实现简单的文件上传功能:
第一步:将如下文件引入到WEB_INF/lib目录下面,对应的jar文件可自行下载

第二步:在包test.struts2下建立类UploadFile
package test.struts2; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class UploadFile { private File file;
private String fileFileName;
private String fileContentType;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String upLoad() throws IOException
{
String realpath=ServletActionContext.getServletContext().getRealPath("/File");
if(file!=null)
{
File savefile=new File(new File(realpath),fileFileName);
if(!savefile.getParentFile().exists())savefile.getParentFile().mkdirs();
FileUtils.copyFile(file, savefile);
System.out.println(realpath);
ActionContext.getContext().put("message", "上传成功"); }
else
{
ActionContext.getContext().put("message", "上传失败");
}
return "upload";
}
}
第三步:在struts.xml文件中添加如下代码
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="test1" extends="struts-default">
<action name="uploadTest" class="test.struts2.UploadFile" m ethod="upLoad">
<result name="upload">/WEB-INF/page/hello.jsp</result>
</action>
</package>
第四步:在Webroot下建立upload.jsp
<body>
<form action="${pageContext.request.contextPath }/test1/uploadTest.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file">
<input type=submit value="上传">
</form>
</body>
第五步:在hello.jsp文件中添加 ${message}
重新部署之后:



下图为System.out.println(realpath);的输出结果(即上传后文件的绝对路径)

struts2实现文件上传的更多相关文章
- struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- jsp\struts1.2\struts2 中文件上传(转)
jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...
- Struts2+Uploadify文件上传使用详解
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...
- Struts2 多文件上传
Struts2多文件上传只需要将 单文件上传中的File变成File[] 即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...
- Struts2图片文件上传,判断图片格式和图片大小
1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- Struts2实现文件上传(四)
Struts2实现文件上传 配置文件struts.xml <!-- /* * $Id: struts.xml 1364077 2012-07-21 12:57:02Z lukaszlenart ...
- Struts2实现文件上传(三)
Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- Struts2实现文件上传(二)
Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" ...
- Struts2实现文件上传(一)
Struts2实现文件上传 文件上传成功后结果页面 result.jsp: <%@ page language="java" import="java.util.* ...
随机推荐
- Arcgis Engine最短路径分析
ArcEngine 最短路径分析(源码) using System; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; using ESRI ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...
- NSSize
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasep ...
- 在屏幕上显示C盘根目录下的所有文件和文件夹
1 import java.io.File; //在屏幕上显示C盘根目录下的所有文件和文件夹 public class ListDemo { public static void main(Strin ...
- 机器学习 —— 概率图模型(CPD)
CPD是conditional probability distribution的缩写,翻译成中文叫做 条件概率分布.在概率图中,条件概率分布是一个非常重要的概念.因为概率图研究的是随机变量之间的练习 ...
- Spring IoC — 基于XML的配置
1.属性注入 注意点: 1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常. 2)JavaBean关于属性命名的特殊规范.Spring只会检查B ...
- 英文论文写作之讨论与结论Discussion and Conclusion
Discussion and Conclusion After viewing these maps, what should immediately appear is the level of r ...
- linux系统的文件类型学习
linux是一个文件型操作系统,在linux下一切皆文件. 目录.字符设备.块设备.管道.套接字.符号连接文件等在linux下统统都是文件. linux下的文件类型分为以下几种类型: 1. 正规文件, ...
- 对于接收到的GPS信息详解
最近一直在做gps驱动方面的东西,对于底层接收到的gps信息不是很了解,查询了资料对这些信息做出总结: 由于在室内,所以信号不是很好,接收不到卫星信号,必须站到窗口或者空旷的地方,这是gps的debu ...
- Android权限安全(4)在什么时候检验权限?
Android独有的Service等 : 通过PM的CheckPermission 其中 pm 是package manager services 非Android特有的Service等 : 映射为O ...