关于在Struts2框架下实现文件的上传功能
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框架下实现文件的上传功能的更多相关文章
- 在SpringMVC框架下实现文件的 上传和 下载
在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...
- Struts2框架下的文件上传文件类型、名称约定
Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...
- JavaWeb框架_Struts2_(七)----->文件的上传和下载
这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 1. 章节目录 Struts2文件上传 单文件上传 拦截器实现文件过滤 文件上传常量 ...
- [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts2基础学习(六)—文件的上传和下载
一.文件的上传 1.单个文件上传 Struts2使用拦截器完成了文件的上传,而且底层使用的也是FileUpload开源组件. 客户端注意事项: (1)method="post&qu ...
- android下大文件分割上传
由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...
- asp.net 如何实现大文件断点上传功能?
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- asynchttpClient框架关于多文件批量上传的问题,改用xUtil
RequestParams params = new RequestParams(); params.add("ordernum",ordernum); params.add(&q ...
随机推荐
- install python module
[install python module] 参考:http://docs.python.org/2.7/install/index.html
- Python pexpec 解决scp ssh
paswd_key = '.*assword.*' 匹配Password ssh_newkey = '.*(yes/no).*' 匹配 Are you sure you want to contin ...
- win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
第一个错误: 安装过程依旧是那样简单,但在配好IIS站点,准备连接数据库的时候出错了,以下是错误提示:System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更 ...
- Mac生存手册
最近刚从Linux转到了Mac系统上,感觉好的地方是再也不折腾了,什么GNOME, KDE, XFCE,各种发行版本都远离我而去了.当然Mac下很多好软件都是要付费的,我只能绕着走了: 1. 命令行, ...
- Windows 10 Edge浏览器、照片查看程序关闭“平滑滚动”
升级到10后,这两个常用软件的“平滑滚动”功能,个人感觉体验有点不好,特别是图片这个自带程序,看了几十张图后就有点头晕了,所以把它关闭为好: 控制面板\系统和安全\系统\高级系统设置\高级\性能\设置 ...
- JAVA 开发实例 一 移动的小球
package com.java.move; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; i ...
- stl map高效遍历删除的方法 [转]
for(:iter!=mapStudent.end():) { if((iter->second)>=aa) { //满足删除条件,删除当前结点,并指 ...
- DOM2
DOM级别 文档类型: 节点类型: 判断节点类型(注意Node对象): <div id="container">这是一个元素节点</div> <scr ...
- 移动端类似IOS的滚动年月控件(需要jQuery和iScroll)
一. 效果图 二. 功能介绍 支持滚动和点击选择年月.(目前只支持设置年月的最大最小值,不支持整体的最大最小值) 三. 代码 1. 在你的html中添加如下代码: 直接加载<body>里面 ...
- (算法)N皇后问题
题目: 八皇后问题:在8 X 8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处于同一行,同一列或者同意对角线上,求出所有符合条件的摆法. 思路: 1.回溯法 数据结构: 由于8个皇后 ...