struts2的配置过程

(1)在项目中加入jar包

(2)web.xml中filter(过滤器)的配置

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

(3)struts.xml配置文件的编写

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

(4)action类的编写

package com.xmgc.sc.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.struts2.ServletActionContext; public class MyUpLoadAction { private String title;
private File upload;//与form表单中的file文件类型的名字是一样的
private String uploadContentType;//前缀必须要是上面的upload
private String uploadFileName; public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} /* public String getSavePath() {
//ServletContext cxt=ServletActionContext.getServletContext();
//String path=cxt.getRealPath("/");
//这个获取的path为:http://localhost:8080/sc
//上传以后变为E:\software\apache-tomcat-6.0.45\webapps\sc return savePath; } public void setSavePath(String savePath) {
//E:\software\apache-tomcat-6.0.45\webapps\sc/myupload
this.savePath = ServletActionContext.getServletContext().getRealPath("/myupload");
}*/ public String execute() throws IOException{ System.out.println(title);//标题
System.out.println(uploadContentType);//准备上传的文件的文件类型
System.out.println(uploadFileName);//准备上传的文件的文件名,记住还有扩展名
System.out.println(upload); String realPath=ServletActionContext.getServletContext().getRealPath("/"); String path=realPath+"myupload/"+uploadFileName; System.out.println(realPath);
System.out.println(path); FileInputStream fis=new FileInputStream(upload);
FileOutputStream fos=new FileOutputStream(path); byte[] bytes=new byte[1024];//定义一个1024大小的字节数组
int len=-1;//用来做标志位 while((len=fis.read(bytes))>0){
fos.write(bytes, 0, len);
} return null;
}
}

(5)JSP页面的编写

<%@ page contentType="text/html;charset=utf-8"%>

<!-- enctype="multipart/form-data",这个是最重要的配置 -->
<form action="myupload.action" enctype="multipart/form-data" method="post">
文件名:<input type="text" name="title"/><br/>
上传:<input type="file" name="upload"/><br/>
<input type="submit" value="上传"/>
</form>

  

经过这次总结,感觉struts2框架下的单文件的上传还是非常简单的,只要获取要存储在什么地方的地址,再通过输入输出流,写到这个地址中去就行了。绝大部分工作,struts2已经帮我们做好了

关于在Struts2框架下实现文件的上传功能的更多相关文章

  1. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  2. Struts2框架下的文件上传文件类型、名称约定

    Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...

  3. JavaWeb框架_Struts2_(七)----->文件的上传和下载

    这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 1. 章节目录 Struts2文件上传 单文件上传 拦截器实现文件过滤 文件上传常量 ...

  4. [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Struts2基础学习(六)—文件的上传和下载

    一.文件的上传 1.单个文件上传      Struts2使用拦截器完成了文件的上传,而且底层使用的也是FileUpload开源组件. 客户端注意事项: (1)method="post&qu ...

  6. android下大文件分割上传

    由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...

  7. asp.net 如何实现大文件断点上传功能?

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  8. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...

  9. asynchttpClient框架关于多文件批量上传的问题,改用xUtil

    RequestParams params = new RequestParams(); params.add("ordernum",ordernum); params.add(&q ...

随机推荐

  1. NHibernate的简单例子

    NHibernate的简单例子 @(编程) [TOC] 因为项目需求,搭了一个NHibernate的例子,中间遇到了一些问题,通过各种方法解决了,在这里记录一下最后的结果. 1. 需要的dll Com ...

  2. django 命名空间详解

    include(module[, namespace=None, app_name=None ]) include(pattern_list) include((pattern_list, app_n ...

  3. Maven+Spring Batch+Apache Commons VF学习

    Apache Commons VFS资料:例子:http://www.zihou.me/html/2011/04/12/3377.html详细例子:http://p7engqingyang.iteye ...

  4. 使用WITH AS 的ROW_NUMBER分页

    WITH tempTable AS(     --复杂查询语句) SELECT * FROM (select ROW_NUMBER()  Over( order by xxx) as rowNum, ...

  5. 关于select @@IDENTITY的初识

    这句话主要是得到唯一的主键,然后应用于下面的SQL语句 例如代码 StringBuilder strSql=new StringBuilder(); strSql.Append("inser ...

  6. 如何让ASP.NET网站站点不停止 永远持续运行

    转载自  http://www.cnblogs.com/insus/p/3658752.html 在公司内部服务器,运行很多网站(应用程序),但每个网站都有自动化或是定时执行的事务.后来经整合,所有这 ...

  7. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  8. Web Service实现分布式服务的基本原理

    简单的说, 就是客户端根据WSDL 生成 SOAP 的请求消息, 通过 HTTP 传输方式(也可以是其它传输方式, 如 FTP 或STMP 等,目前 HTTP 传输方式已经成为 J2EE Web Se ...

  9. RFID Reader 线路图收集

    This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...

  10. START167 AND BOOT167

    http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka10535.html C166: START167 AND BOO ...