首先配置一下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

新建一个上传页面:upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
file:<input type="file" name="file" /><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>

UploadAction.java:

package com.struts2.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private static final long serialVersionUID = 1L; /** 文件 */
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;
} @Override
public String execute() throws Exception { String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/upload"); InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(uploadPath,
this.fileFileName)); int length = 0;
byte[] buffer = new byte[1024];
while (-1 != (length = is.read(buffer))) {
os.write(buffer, 0, length);
}
is.close();
os.close(); return SUCCESS;
}
}

上传成功后的页面uploadResult.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
upload successfully!
</body>
</html>

最后配置一下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.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="104857600" /> <package name="struts2" extends="struts-default"> <!-- 单文件上传 -->
<action name="upload" class="com.struts2.action.UploadAction">
<result name="success">/uploadResult.jsp</result>
</action> </package>

Struts2实现单文件上传的更多相关文章

  1. struts2之单文件上传(7)

    前台页面jsp <!-- 拦截的时候用这个 <s:form action="uploadAction" enctype="multipart/form-dat ...

  2. Struts2单文件上传原理及示例

    一.文件上传的原理 表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值: 1.application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里 ...

  3. 【Struts2学习笔记(9)】单文件上传和多文件上传

    (1)单文件上传 第一步:在WEB-INF/lib下增加commons-fileupload-1.2.1.jar.commons-io-1.3.2.jar. 这两个文件能够从http://common ...

  4. Struts2 单文件上传

    Struts2 提供了更为简便的文件上传机制,将文件上传的复杂操作都封装到commons-fileupload.jar .commons-io.jar两个jar包中,然后再程序中使用简单的几句代码就能 ...

  5. Struts2 之 实现文件上传和下载

    Struts2  之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...

  6. ajaxFileUpload+struts2实现多文件上传

    以前有介绍过ajaxFileUpload实现文件上传,但那是单文件的,这次介绍多文件上传. 单文件上传参考:http://blog.csdn.net/itmyhome1990/article/deta ...

  7. 关于Struts2的多文件上传

    之前写过一篇文章,关于Struts2文件上传:http://www.cnblogs.com/lichenwei/p/3927964.html 现在来说下多文件上传,其实就把上传文件当成是一个数组去处理 ...

  8. [转]Struts2多个文件上传

    转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...

  9. 笨鸟先飞之Java(一)--使用struts2框架实现文件上传

    无论是.net还是Java,我们最常接触到的就是文件的上传和下载功能,在Java里要实现这两个经常使用功能会有非常多种解决方案,可是struts2的框架却能给我们一个比較简单的方式,以下就一起来看吧: ...

随机推荐

  1. python学习之路-12

    线程池 上下文管理 线程池中关于上下文管理的相关代码 点我查看更详细的上下文管理介绍 import contextlib @contextlib.contextmanager def worker_s ...

  2. ZooKeeper的学习与应用

    近期大概学习了一下ZooKeeper,本身并没有深入.LGG尝试着在虚拟机里面搭了平台,看了看一些教材,从网上到处看别人的博文并引用之,还请各位大牛们谅解我的剽窃.现总结例如以下. 1. ZooKee ...

  3. container宽度

    bootstrap:宽度太宽时无需改变container的宽度大小,只需:.row{margin-left: 30px;margin-right: 30px;}

  4. js实现求平均数功能

    今天在项目中遇到了一个求平均值的需求,大致需求就是,页面上面有四个input框,失去焦点就计算平均值,结果保留两位小数,并输出在页面上.不多说了,直接奉上代码,如有更好思路或者想法,都欢迎大家和我讨论 ...

  5. asp.net 通过js调用webService注意

    通过JavaSrcipt调用WebService格式: //通过SricptManager 的,services标签添加web服务引用 <asp:ScriptManager runat=&quo ...

  6. java中的io系统详解(转)

    Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...

  7. Objective C—创建单例

    单例模式是在实际项目开发中用到比较多的一种设计模式,设计原理是整个系统只产生一个对象实例,通过一个统一的方法对外提供这个实例给外部使用. 在Java中,构造单例一般将类的构造函数声明为private类 ...

  8. R语言学习笔记(数据预处理)

    setwd("d:/r/r-data/")data=read.table("salary.txt",header=T)attach(data)mean(Sala ...

  9. python-整理--sqlite数据库访问

    python 自带sqlite3数据库访问模块. sqlite3 以下写一个数据库访问类 ''' 2016年2月5日 描述: 操作sqlite数据库的封装 主要功能: 将sqlite数据库数据转为py ...

  10. gulp压缩js

    1.安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 2.查看nodejs的版本号 3.npm ...