一.单个文件上传

文件上传需要两个jar包:

首先制作一个简单的页面,用于实现文件上传

<h1>单个文件上传</h1>
<s:form action="upload.action" enctype="multipart/form-data"
method="post" namespace="/">
<s:textfield name="title" lable="标题"></s:textfield>
<s:file name="upload" lable="选择文件"></s:file>
<s:submit value="上传文件"></s:submit>
</s:form>

开发实现文件上传的Action

package cn.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport{
//封装上传文件属性
private File upload; //封装上传文件的类型
private String uploadContentType; //封装上传文件名称
private String uploadFileName; //封装文件上传的路径
private String savePath; public String execute(){
byte[] buffer=new byte[1024];
try {
FileInputStream fis=new FileInputStream(getUpload());
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName());
int length=fis.read(buffer);
while(length>0){
fos.write(buffer, 0, length);
length=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
System.out.println("========================");
return SUCCESS;
} 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() {
return ServletActionContext.getServletContext().getRealPath(savePath);
} public void setSavePath(String savePath) {
this.savePath = savePath;
} }

在Action中使用了三个属性封装文件信息

File类型的XXX属性,与表单的File控件的name属性一样,用于封装File控件对应的文件内容

String类型的xxxFileName属性,该属性名称由前面的File类型属性和FileName组合,是固定的语法,是封装File控件对应文件的文件名

String类型的XXXContentType属性,同样由xxx属性和ContentType组合而成,是固定语法,封装File控件对应文件的文件类型

配置Action

<!-- 单个文件上传 -->
<action name="upload" class="cn.action.UploadAction">
<!-- 通过param参数设置保存目录的路径 -->
<param name="savePath">/upload</param>
<result name="success">success.jsp</result>
</action>

效果图:

二.多个文件上传

只需在上传Action中将原本处理单个文件的操作改成对集合操作即可。

其他的都跟单个上传的一样

页面

<!-- 多个文件上传 -->
<action name="someupload" class="cn.action.SomeUploadAction">
<!-- 通过param参数设置保存目录的路径 -->
<param name="savePath">/upload</param>
<result name="success">success.jsp</result>
</action>

开发实现文件上传的Action

package cn.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class SomeUploadAction extends ActionSupport {
//封装上传文件属性
private File[] upload; //封装上传文件的类型
private String[] uploadContentType; //封装上传文件名称
private String[] uploadFileName; //封装文件上传的路径
private String savePath; public String execute() throws Exception{
byte[] buffer=new byte[1024];
for (int i = 0; i < upload.length; i++) {
FileInputStream fis=new FileInputStream(getUpload()[i]);
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName()[i]);
int length=fis.read(buffer);
while(length>0){
fos.write(buffer, 0, length);
length=fis.read(buffer);
}
fos.flush();
fos.close();
fis.close();
}
return SUCCESS;
} 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() {
return ServletActionContext.getServletContext().getRealPath(savePath);
} public void setSavePath(String savePath) {
this.savePath = savePath;
} }

配置Action

<!-- 多个文件上传 -->
<action name="someupload" class="cn.action.SomeUploadAction">
<!-- 通过param参数设置保存目录的路径 -->
<param name="savePath">/upload</param>
<result name="success">success.jsp</result>
</action>

效果:

三.文件下载

文件下载需要InputStream,首先在文件下载Action中提供一个获得InputStream的方法,通过输入流可以获取希望下载的文件内容

package cn.action;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileDownAction extends ActionSupport {
//读取下载文件的目录
private String inputPath; //下载文件的文件名
private String fileName; //读取下载文件的输入流
private InputStream inputStream; //下载文件的类型
private String conetntType; public String execute(){
return SUCCESS; } public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}
//创建InputStream输入流
public InputStream getInputStream() throws Exception {
String path=ServletActionContext.getServletContext().getRealPath(inputPath);
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
return stream;
} public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
} public String getConetntType() {
return conetntType;
} public void setConetntType(String conetntType) {
this.conetntType = conetntType;
} }

通过Context得到下载文件的实际路径,构建一个InputStream输入流实现文件的下载读取。

在配置文件中,同样对Action进行配置,并对stream结果类型的参数进行设置。

<!-- download指定的Action -->
<action name="download" class="cn.action.FileDownAction">
<param name="inputPath">/upload</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>

ContentType参数决定了下载文件的类型,不同的文件类型对应的参数值也是不同的。

通常情况下,ContentType参数直接设置为application/octet-stream即可。

contentDisposition参数由两部分组成,前面的部分表示处理文件的形式,如attachement表示在下载时弹出对话框,提出用户保存或直接打开该文件;而后一部分表示下载文件的文件名称。两部分之间用“;”进行分隔。

然后开发一个简单的下载页面,在页面中设置一个超链接,通过超链接请求下载Action

<h1>文件下载</h1>
<s:a href="download.action?fileName=2.jpg">点击此处下载文件</s:a>

效果:

Struts2文件上传和文件下载的更多相关文章

  1. 分享知识-快乐自己:Struts2文件上传及文件下载

    1)Struts2单文件上传 action:类文件 package com.mlq.action; import com.opensymphony.xwork2.ActionSupport; impo ...

  2. Struts2 文件上传和文件下载

    一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="uplo ...

  3. struts2的文件上传和文件下载

    实现使用Struts2文件上传和文件下载: 注意点: (1)对应表单的file1和私有成员变量的名称必须一致 <input type="file" name="fi ...

  4. Struts2文件上传下载

    Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...

  5. Struts2文件上传和下载(原理)

    转自:http://zhou568xiao.iteye.com/blog/220732 1.    文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1)     ...

  6. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  7. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  8. (八)Struts2 文件上传和下载

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 文件上传 Struts2 文件上传基于Struts2 拦 ...

  9. 学习Struts--Chap07:Struts2文件上传和下载

    1.struts2文件上传 1.1.struts2文件上传的基本概述 在开发web应用的时候,我们一般会为用户提供文件上传的功能,比如用户上传一张图像作为头像等.为了能上传文件,我们必须将表单的met ...

随机推荐

  1. 基于Eclipse的Hadoop应用开发环境配置

    基于Eclipse的Hadoop应用开发环境配置 我的开发环境: 操作系统ubuntu11.10 单机模式 Hadoop版本:hadoop-0.20.1 Eclipse版本:eclipse-java- ...

  2. 疯狂Android讲义 - 学习笔记(五)

    第五章 Android使用统一的Intent对象来封装“启动意图”,不管是启动Activity.Service组件.或者BroadcastReceiver等,提供了一致的编程模型.Intent设计有点 ...

  3. virtualbox 虚拟机Ubuntu 传文件-共享

  4. GA算法-R语言实现

    旅行商问题 北工商-经研143班共有30位同学,来自22个地区,我们希望在假期来一次说走就走的旅行,将所有同学的家乡走一遍.算起来,路费是一笔很大的花销,所以希望设计一个旅行方案,确保这一趟走下来的总 ...

  5. jQuery:详解jQuery中的事件(二)

    上一篇讲到jQuery中的事件,深入学习了加载DOM和事件绑定的相关知识,这篇主要深入讨论jQuery事件中的合成事件.事件冒泡和事件移除等内容. 接上篇jQuery:详解jQuery中的事件(一) ...

  6. jquery和css3实现滑动导航菜单

    效果预览:http://keleyi.com/keleyi/phtml/html5/15/ 有都中颜色可供选择,请使用支持HTML5/CSS3的浏览器访问. HTML源代码: <!DOCTYPE ...

  7. if语句,case语句

    1.句式:if...then.判断赋值 例: if RadioButton1.Checked then sex:='男' else if RadioButton2.Checked then sex:= ...

  8. 【requireJS路径加载】与程序员小卡的交流

    这两天正好看到了程序员小卡同学的一篇博客,里面对requireJS路径的解析做了一些说明,里面有点问题待解决,我这里正好知道一点,所以整理成文,不知对小卡同学是否有帮助. http://www.cnb ...

  9. IIS6.0添加上.net4.0后,以前的.net系统出现“服务器应用程序不可用”的错误提示解决办法

    把VS2010开发的网站.net4.0部署到Windows Server 2003的服务器上去, Windows Server 2003操作系统自带的为IIS 6.0,IIS 6.0一般只支持.NET ...

  10. CSS常用背景图片定位方法

    CSS背景图片定位其实对于每一位学习前端的同学来说,都已经非常熟悉了.网上铺天盖地的最常见的一种方案就是在父元素中relative,然后子元素absolute.这种方案当然好,不过带来的一个缺点就是会 ...