FileExportUtils
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
public class FileExportUtils {
private static HttpServletRequest excelRequest = null;
private static HttpServletResponse excelResponse = null;
private static FileOutputStream fileOut;
public FileExportUtils(PageContext context) {
this.excelRequest = (HttpServletRequest) context.getRequest();
this.excelResponse = (HttpServletResponse) context.getResponse();
}
public FileExportUtils(HttpServletRequest request,
HttpServletResponse response) {
excelRequest = request;
excelResponse = response;
}
public void outputTxt(String excelName, String resultStr) {
//String realPath = "c:\\"+excelName+".xls";
String realPath = getExcelRealPath(excelName);
writerFileStream(realPath,resultStr);
downloadFile(excelName);
}
private static String getExcelRealPath(String excelName) {
String realPath = excelRequest.getRealPath("/UploadFile");
File excelFile = new File(realPath);
if(!excelFile.exists()) {
excelFile.mkdirs();
}
excelName = realPath+ "\\" + excelName+".txt";
return excelName;
}
private static void downloadFile(String strfileName) {
try {
// 获得ServletContext对象
if(excelFileNotFund(strfileName)) {
throw new IllegalArgumentException("File=["+strfileName+"] not fund file path");
}
// 取得文件的绝对路径
File excelFile = getExcelDownloadPath(strfileName);
putResponseStream(strfileName, excelFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param strfileName : 文件名称
* @param excelName : 文件的相对路径或绝对路径
* @throws UnsupportedEncodingException
* @throws FileNotFoundException
* @throws IOException
*/
private static void putResponseStream(String strfileName, File excelName)
throws UnsupportedEncodingException, FileNotFoundException,
IOException {
strfileName = URLEncoder.encode(strfileName, "UTF-8");
excelResponse.setHeader("Content-disposition","attachment; filename=" + strfileName+".txt");
excelResponse.setContentLength((int) excelName.length());
// excelResponse.setContentType("application/x-download");
// excelResponse.setContentType("application/vnd.ms-Excel");
excelResponse.setContentType("application/x-msdownload");
byte[] buffer = new byte[4096];
int i = 0;
FileInputStream fis = new FileInputStream(excelName);
while ((i = fis.read(buffer,0,4096)) > 0) {
JspWriter out = null;
excelResponse.getOutputStream().write(buffer, 0, i);
}
excelResponse.flushBuffer();
}
//用传入参数的判断
private static boolean excelFileNotFund(String strfileName) {
return strfileName == null|| strfileName.equals("");
}
public static File getExcelDownloadPath(String excelName) {
String realPath = excelRequest.getRealPath("/UploadFile");
excelName = realPath+ "\\" + excelName+".txt";
File excelFile = new File(excelName);
return excelFile;
}
/**
* 输入当前WorkBook为下载临时文件记录
*
* @param excelName
*/
public void writerFileStream(String excelName,String resultStr) {
try {
fileOut = new FileOutputStream(excelName);
fileOut.write(resultStr.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOut.flush();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//FileOutputStream fis = new FileOutputStream();
}
}
分享到:
相关推荐
文件实用程序 FileUtil 是库项目,用于在内部和外部存储中或从内部和外部存储中读取和写入文件(byteArray)。 如何在代码中使用此库 - 用于在内部存储中/从内部存储中读取和写入文件中的字符串 ...
将此行添加到应用程序的 Gemfile 中: gem 's3_utils' 然后执行: $ bundle 或者自己安装: $ gem install s3_utils 依赖 用法 require 's3_utils' 这个模块有一些方法。 #upload_to_s3 将本地文件上传到 ...
Android包相关工具类,可用于(root)安装应用、(root)卸载应用、判断是否系统应用等,如: install(Context, String) 安装应用,如果是系统应用或已经root,则静默安装,否则一般安装 uninstall(Context, String) ...
将此行添加到应用程序的 Gemfile 中: gem 'lynda-translator-srt' 或者从 github 添加最新版本: gem 'lynda-translator-srt', git: 'https://github.com/demetrodon/lynda-translator-srt' 然后执行: $ ...
它提供替代了你通常在GNU fileutils,shellutils等中发现的部分实用工具。 BusyBox的使用功能选项一般比其堂兄弟全功能的GNU少,然而,选项中包含提供与其功能和GNU非常相似。 BusyBox为任何小型或嵌入式系统提供了一...
忙箱版权所有(C)2015-2019 Anton Skshidlevsky... 该应用程序可从Google Play和GitHub下载。 最新的BusyBox v1.31.1,支持374个applet: [, [[, acpid, adjtimex, ar, arch, arp, ash, awk, base64, basename, bbconf
DBUtils.psm1 基本的数据库操作-备份/还原/执行等创建数据库还原数据库备份数据库附加数据库执行文件FileUtils.psm1 用于下载/提取/打包文件等下载文件提取包装设置权限解压缩存档Add-HostFileContent ConfigUtils....
动机尽管Ruby的File和FileUtils库提供了用于处理文件的非常健壮的api,但是该库的目的是提供一种更加方便的抽象级别,并具有有用的日志记录功能。安装将此行添加到您的应用程序的Gemfile中: gem "tty-file" 然后...
FileUtils 文件操作 HanziToPinyin 拼音汉字处理 IOUtils IOUtils MD5 MD5 MiscUtils 设备信息的获取 NetWorkUtils 网络状态 PhoneUtil 手机组件调用工具类 PreferencesUtils sp工具类 RandomUtils 随机数工具类 ...
材料: 库下载以使应用程序正常工作FileUtils: : Weka来源: : 番石榴: ://search.maven.org/remotecontent?filepath com/google/guava/guava/23.0/guava-23.0.jar 适用于0.1版测试文档的草图: :
例如:FileUtils.writeStringToFile(outFile, result, "UTF-8"); 只一句代码就可以将一个字符串写入一个文件,如果我们自己写的话,得这样 打开一个文件,判断是否存在,判断编码类型,写入方式是追加还是覆盖,...
文件相关 -> FileUtils.java -> Test getFileByPath : 根据文件路径获取文件 isFileExists : 判断文件是否存在 rename : 重命名文件 isDir : 判断是否是目录 isFile : 判断是否是文件 createOrExistsDir : 判断目录...
例如,它使 FileUtils 方法直接在构建脚本上下文中可用。 大多数情况下,它只是相信开发人员知道如何编写他们需要的构建脚本。 您将在下面找到一个简短的“热分钟”指南,用于快速启动和运行 Rulebow。 这足以让您...
ToolLibrary 工具类 ...文件读写:包括任意位置的读写,和应用目录下file目录下的读写,覆盖写和追加写 4、GlideUtils 图片加载工具类 通过Glide加载图片,内部包含多个方法,如是否需要圆形加载,是否加
本书主要帮助读者了解如何通过自行开发应用程序来监视或者控制仪器硬件。本书内容涵盖了从接线到建立接口,直到完成可用软件的整个过程。 本书适合需要进行仪表控制、机器人、数据采集、过程控制等相关工作的读者...