利用Struts2 实现文件下载
2023-09-14 09:01:03 时间
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://dba10g.blog.51cto.com/764602/851613
* $Id: FileDownloadAction.java 496318 2007-01-15 13:58:24Z husted $
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.showcase.filedownload;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action {
private String inputPath;
private String fileName;
private String contentType;
/**
* 输入流
*/
private InputStream inputStream;
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setInputPath(String value) {
inputPath = value;
}
/**
* 如果设置了inputStream 则将inputStream输出到浏览器
* 如果通过inputPath获取文件,并将文件输出至浏览器;
* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception {
if(inputStream != null){
return inputStream;
}
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String execute() throws Exception {
this.setContentType("application/vnd.ms-excel");
this.setFileName("wk.xls");
/**
* 生成Excls文件
*/
if(StringUtils.isBlank(inputPath)){
ByteArrayOutputStream output = new ByteArrayOutputStream();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet 2");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue("This is a string");
row.createCell(3).setCellValue(true);
wb.write(output);
InputStream is = new ByteArrayInputStream(output.toByteArray());
this.setInputStream(is);
}
// return is;
return SUCCESS;
}
}
action name="download2"
result name="success" type="stream"
param name="contentType" ${contentType} /param
param name="inputName" inputStream /param
param name="contentDisposition" filename=${fileName} /param
param name="bufferSize" 4096 /param
/result
/action
遗留问题:中文名称; Struts 如果是图片等等 会默认打开。稍后完善
struts2的文件上传 在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在...
try { String path = new String(filePath.getBytes( ISO-8859-1 ), utf-8 //处理get请求传过来的中文参数乱码,filePath文件路径需要set方法接收页面参数 System.out.println(path)
Struts2文件上传 1 在Struts2中上传文件需要 commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar 这两个包。 2 确认页面form表单上的提交方式为POST,enctype属性的值为“multipart/form-data”。
* $Id: FileDownloadAction.java 496318 2007-01-15 13:58:24Z husted $
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.showcase.filedownload;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* Demonstrates file resource download.
* Set filePath to the local file resource to download,
* relative to the application root ("/images/struts.gif").
*
*/
public class FileDownloadAction implements Action {
private String inputPath;
private String fileName;
private String contentType;
/**
* 输入流
*/
private InputStream inputStream;
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void setInputPath(String value) {
inputPath = value;
}
/**
* 如果设置了inputStream 则将inputStream输出到浏览器
* 如果通过inputPath获取文件,并将文件输出至浏览器;
* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception {
if(inputStream != null){
return inputStream;
}
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String execute() throws Exception {
this.setContentType("application/vnd.ms-excel");
this.setFileName("wk.xls");
/**
* 生成Excls文件
*/
if(StringUtils.isBlank(inputPath)){
ByteArrayOutputStream output = new ByteArrayOutputStream();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet 2");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue("This is a string");
row.createCell(3).setCellValue(true);
wb.write(output);
InputStream is = new ByteArrayInputStream(output.toByteArray());
this.setInputStream(is);
}
// return is;
return SUCCESS;
}
}
action name="download2"
result name="success" type="stream"
param name="contentType" ${contentType} /param
param name="inputName" inputStream /param
param name="contentDisposition" filename=${fileName} /param
param name="bufferSize" 4096 /param
/result
/action
遗留问题:中文名称; Struts 如果是图片等等 会默认打开。稍后完善
本文出自 “简单” 博客,请务必保留此出处http://dba10g.blog.51cto.com/764602/851613
struts2的文件上传 在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来比较繁琐,而且不灵活,在学习了struts2后,struts2为文件上传下载提供了更好的实现机制,在...
try { String path = new String(filePath.getBytes( ISO-8859-1 ), utf-8 //处理get请求传过来的中文参数乱码,filePath文件路径需要set方法接收页面参数 System.out.println(path)
Struts2文件上传 1 在Struts2中上传文件需要 commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar 这两个包。 2 确认页面form表单上的提交方式为POST,enctype属性的值为“multipart/form-data”。
相关文章
- FTP下载文件失败
- Everything 文件搜索神器
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
- Struts文件上传与下载详解_文件的下载
- Centos 与本地终端 上传、下载 文件
- windows环境通过cmd命令到ftp上下载文件到linux服务器
- Linux中使用SecureCRT上传、下载文件命令sz与rz用法实例
- servlet 中通过response下载文件
- fastDFS分布式文件系统--文件上传/下载/查询完整代码实现
- 使用springMVC实现文件上传和下载之环境配置与上传
- 如何通过云存储实现大文件的断点下载和上传
- haarcascades---各种分类器xml文件下载地址
- 【Android 逆向】修改 Android 的 apk 安装包内的文件并重新打包 ( apktool_2.6.0.jar 下载和使用 | zipalign 文件对齐 | apksigner 签名 )
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
- Linux安装conda Ubuntu安装conda Linux安装百度云 Ubuntu安装百度云 服务器安装百度云 Linux上传到百度云 Linux上传文件到百度云 Linux从百度云下载
- 如何获得基因名与基因类型的对应关系——下载GTF文件 基因注释
- 百度文库下载文件,没有财富的价值,音量不能下载
- 前端必备技能知识:JS导出Blob流文件为Excel表格、Vue.js使用Blob的方式实现excel表格的下载(流文件下载)
- Python下载文件进度条Demo
- Maven下载jar包时出错、Maven仓库中出现.lastUpdated结尾的文件问题及解决
- 基于Cookie-Editor与curl实现跨设备的文件下载