Struts2--文件的下载

通过个人的struts2案例,了解struts2如何下载文件

1.<!-- 在action中配置-->

<result name="success" type="stream">

//下载的文件类型

<param name="contentType">image/jpeg</param>

//文件输入流来源

<param name="inputName">imageStream</param>

//设置保存文件的文件名

<param name="contentDisposition">attachment;filename="document.pdf"</param>

//缓冲区大小

<param name="bufferSize">1024</param>

</result>

1.1.以上参数可以通过 Action 中的getter方式提供值!<不必再result中设置参数了>

 1.2以下案例通过getter给变量赋值

2.javabean 具体实现代码 (案例)

package com.cn.web;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadFile extends ActionSupport {

/**

* struts2 文件下载图片

*/

private static final long serialVersionUID = 1L;

// 获取文件名称

private String imgname;

// 下载的文件类型

private String contentType;

// 下载的文件长度

private long contentLength;

// 设置保存文件的文件名

private String contentDisposition;

// 文件输入流来源

private InputStream inputStream;

public String getContentType() {

return contentType;

}

public void setContentType(String contentType) {

this.contentType = contentType;

}

public long getContentLength() {

return contentLength;

}

public void setContentLength(long contentLength) {

this.contentLength = contentLength;

}

public String getContentDisposition() {

return contentDisposition;

}

public void setContentDisposition(String contentDisposition) {

this.contentDisposition = contentDisposition;

}

public String getImgname() {

return imgname;

}

public void setImgname(String imgname) {

this.imgname = imgname;

}

public InputStream getInputStream() {

return inputStream;

}

public void setInputStream(InputStream inputStream) {

this.inputStream = inputStream;

}

// 获取下载的视图

public String downloadUI() {

return "downloadUI";

}

public String execute() throws IOException {

// 确定各个成员变量的值

contentType = "image/jpeg";

contentDisposition = "attachment;filename=" + imgname + ".jpg";

String newsrc = ServletActionContext.getServletContext().getRealPath(

"/WEB-INF/upload" + File.separator + imgname + ".jpg");

System.out.println(newsrc);

inputStream = new FileInputStream(newsrc);

contentLength = inputStream.available();

return SUCCESS;

}

}

3.打开struts2.xml文件,在相应的action中配置<result name="success" type="stream"></result> 

<action name="down_*" class="com.cn.web.DownloadFile" method="{1}">

<result name="{1}">/WEB-INF/downloadUI.jsp</result>

<!-- 提供内容给用户下载 -->

<result name="success" type="stream">

<!-- 缓冲区大小 -->

<param name="bufferSize">1024</param>

</result>

</action>


4.html代码

    

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"

contentType="text/html; charset=UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Struts2 文件下载</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

</head>

<body>

<a href="down_execute?imgname=886001c3-335b-4604-b547-a659f55ddf76">文件下载1:</a>

<hr>

<br />

<a href="down_execute?imgname=eff2d51f-192a-451c-be06-050fb6529ffd">文件下载2:</a>

<hr>

<br>

<a href="down_execute?imgname=e813e7ad-18dd-461a-87cf-2b035d41026d">文件下载3:</a>

<br>

<hr>

</body>

</html>

5.有什么不懂得同学,欢迎留言,相互交流,共同进步。

时间: 2024-07-28 15:00:32

Struts2--文件的下载的相关文章

Struts2文件的下载

1.下载登录页面download.jsp 1: <%@ page language="java" contentType="text/html; charset=UTF-8" 2: pageEncoding="UTF-8"%> 3: <%@ taglib prefix="s" uri="/struts-tags" %> 4: <!DOCTYPE html PUBLIC &quo

笔记:Struts2 文件上传和下载

为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传解析器配置 Struts2 没有提供自己的请求解析器,struts2 需要调用其他上传框架来解析二进制数据,struts2 默认使用 jakarta 的 Common-FileUpload 的文件上传框架,需要在 Web 应用的 lib 中增加 commons-io-2.2.jar 和 common

struts2 文件的上传下载 表单的重复提交 自定义拦截器

文件上传中表单的准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置为 post 需添加 <input type=“file”> 字段. Struts 对文件上传的支持 在 Struts 应用程序里, FileUpload 拦截器和 Jakarta Commons FileUpload 组件可以完成文件的上传. 步骤:1. 在 Jsp 页面的文件上传表单里使用

struts2 文件上传下载

四.文件的上传(拦截器)和下载(stream结果类型)(需要练一遍) 1.文件上传 必要前提: a.表单method必须是post: b.enctype取值必须是multipart/form-data: c.提供文件选择域. 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-t

4、struts2 文件上传下载

1.文件上传 struts提交的文件组件上传, 前台: 1.提交方式POST 2.表单类型 multipart/form-data 3.input type=file 后台: Apache提供的FileUpload组件 核心类: FileItemFactory FileItem的工厂 ServletFileUpload servlet 中的文件上传的核心类 FileItem 封装了上传的表单文件项的信息 总之 文件上传,处理起来比较麻烦 Struts的文件上传 文件上传拦截器帮助我们晚场了文件上

struts2文件上传下载

文件上传 1.struts2中文件上传介绍 struts2文件上传需要使用apache提供的文件上传组件(commons-fileupload.jar和commons-io.jar). struts2文件上传的核心是通过fileupload拦截器实现的. 2.如何实现文件上传 1>.添加commons-fileupload和commons-io包 2>.在jsp页面做如下配置 将form的method属性值设置为post 给form标记添加属性enctype="multipart/f

【SSH2(实践篇)】--Struts2文件上传下载实例

上篇文章又一次回顾了Struts2的运行机制,对它的运行步骤做了一步步的解析,这个解析不但再一次理清了Struts2的使用方法,而且对它的映射机制进行了深入的解析,并在最后通过一个实例来介绍了Struts2的一种使用方法,这里将做一个有关文件上传下载的实例. 一.文件上传 Struts2并没有提供文件上传的组件,所以想要实现上传的功能就必须通过第三方组件来实现,在Struts2引用的jar中包含了文件上传的组件,它是通过commons-fileupload.jar和commons-io.jar来

struts2实现文件批量下载

大家都知道struts2提供了文件下载的功能,很方便很好用.废话不多说直接开始. 首先我们先对struts.xml进行配置,struts2的result 类型设为stream,请看如下配置: <span style="font-size:18px;"> <result name="toDownload" type="stream"> <param name="bufferSize">2048

struts2学习(13)struts2文件上传和下载(1)

一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte: 例子实现: com.cy.action.FileUploadAction.java: package com.cy.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork

Struts2 文件上传和文件下载

一.单个文件上传 文件上传需要两个jar包: 首先制作一个简单的页面,用于实现文件上传 <h1>单个文件上传</h1> <s:form action="upload.action" enctype="multipart/form-data" method="post" namespace="/"> <s:textfield name="title" lable=&q