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实现文件上传的更多相关文章

  1. struts2的文件上传

    在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...

  2. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  3. Struts2+Uploadify文件上传使用详解

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...

  4. Struts2 多文件上传

    Struts2多文件上传只需要将 单文件上传中的File变成File[]  即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...

  5. Struts2图片文件上传,判断图片格式和图片大小

    1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...

  6. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  7. Struts2实现文件上传(四)

    Struts2实现文件上传 配置文件struts.xml <!-- /* * $Id: struts.xml 1364077 2012-07-21 12:57:02Z lukaszlenart ...

  8. Struts2实现文件上传(三)

    Struts2实现文件上传 配置文件web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  9. Struts2实现文件上传(二)

    Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" ...

  10. Struts2实现文件上传(一)

    Struts2实现文件上传 文件上传成功后结果页面 result.jsp: <%@ page language="java" import="java.util.* ...

随机推荐

  1. PCL—低层次视觉—点云滤波(基于点云频率)

    1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...

  2. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  3. C Socket Programming for Linux with a Server and Client Example Code

    Typically two processes communicate with each other on a single system through one of the following ...

  4. jumplist和changlist

    用jumplist可以在不同的访问过的位置之间跳转 C-O到上一个 C-I到下一个位置 :jumps列出跳转列表 changlist列出最近的改动点 g;到上一个,g,到下一个 :changes列出相 ...

  5. JAVA中,不同工程间的方法调用

    可以调用, 用配置构建路径的方法:点选工程1, 点击右键, 选择 Build Path(构建路径) - > Configure Build Path...(配置构建路径...)然后在弹出的窗口中 ...

  6. Lists of network protocols

    https://en.wikipedia.org/wiki/Lists_of_network_protocols Protocol stack: List of network protocol st ...

  7. Building Xcode iOS projects and creating *.ipa file from the command line

    For our development process of iOS applications, we are using Jenkins set up on the Mac Mini Server, ...

  8. 域名服务器--DNS

    .域名 .DNS.DNS 端口号 .DNS服务器 .域名解析过程及原理 .动态域名解析(DDNS)服务的原理 域名 域名是与主机名称一一对应的一个名字.使得人们可以通过ip的名字来访问ip,域名就是为 ...

  9. Linux“Bash”漏洞大爆发

    9月25日,国外曝出一个“毁灭级”的Bash漏洞,黑客可利用此漏洞远程执行任意命令,完全控制目标系统! 该漏洞编号为CVE-2014-6271,主要存在于bash 1.14 - 4.3版本中,受影响的 ...

  10. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...