package com.cplatform.util.io;
import org.apache.commons.logging.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import com.cplatform.util.business.ParamComm;
public class DownFile extends HttpServlet {
private static org.apache.commons.logging.Log log = LogFactory.getLog(DownFile.class);
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException { }
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String fileName = request.getParameter("fileName"); //取得文件名,包括后辍名,可用File类的getName()方法获得
String filePath = request.getParameter("filePath"); //取得文件路径,服务器端点的绝对路径,如:E:\jsp\。可用File类的getAbsolutePath()方法获得
String saveName = request.getParameter("saveName"); //文件保存在本地的名字
if (log.isDebugEnabled()) {
log.debug("Acquire parameters:");
log.debug("File Name: " + fileName);
log.debug("File Path: " + filePath);
log.debug("Save Name: " + saveName);
}
if (filePath!=null) {
String temp;
if (System.getProperty("file.separator").equals("/")) {
temp = "/";
} else if( filePath.lastIndexOf("/")>0 ){
temp = "/";
} else {
temp = "\\";
}
if (!filePath.endsWith(temp)) {
int position = filePath.lastIndexOf(temp);
fileName = filePath.substring(position+1);
filePath = filePath.substring(0,position+1);
if (log.isDebugEnabled()) {
log.debug("Split file path parameter into file path (" +
filePath + ") and file name (" + fileName + ").");
}
}
//~ 转码的问题已由filter统一处理
// fileName = Chinese.fromPage(fileName);
if (saveName==null || saveName.equals("")) {
saveName = fileName;
}
//~ 转码的问题已由filter统一处理
// saveName = Chinese.toDatabase(saveName);
File file = new File(filePath+fileName);
if (file.exists()) {
response.reset();
response.setContentType("*/*;charset=gbk"); //这里设成 */* 是因为可以让浏览器根据文件的后辍来自动选择一种文件类型
response.setHeader("Content-disposition",
"attachment; filename=" + new String(saveName.getBytes("GBK"),"iso8859-1")); //在下载文件名里显示服务器端的文件名
int size = (int) file.length();
java.io.OutputStream os = response.getOutputStream();
java.io.FileInputStream fis = new java.io.FileInputStream(filePath +
fileName);
int len = 1024;
if (size > (1024 * 1024) && size < (1024 * 1024 * 10)) {
len = 10240;
}
else if (size > (1024 * 1024 * 10)) {
len = 102400;
}
byte[] b = new byte[len];
int i;
try {
while ( (i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
} catch (IOException e) {
if (log.isInfoEnabled()) {
log.info("下载文件出错:" + e.getMessage());
}
} finally {
try { fis.close(); } catch (IOException e) { }
try {
os.flush();
os.close();
} catch (IOException e) { }
}
} else {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>FileDown</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println(
"<script>alert('文件不存在,下载失败!');window.close();</script>");
out.println("</body></html>");
}
// file.delete();//下载后删除源文件
} else {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>FileDown</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println(
"<script>alert('文件参数不全,下载失败!');window.close();</script>");
out.println("</body></html>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {}
}
分享到:
相关推荐
DownLoadUtil.java 和UploadUtil.java简单的工具类 实现文件的上传下载
赠送原API文档:qrcode-utils-1.1-javadoc.jar; 赠送源代码:qrcode-utils-1.1-sources.jar; 赠送Maven依赖信息文件:qrcode-utils-1.1.pom; 包含翻译后的API文档:qrcode-utils-1.1-javadoc-API文档-中文(简体)...
赠送原API文档:qrcode-utils-1.1-javadoc.jar; 赠送源代码:qrcode-utils-1.1-sources.jar; 赠送Maven依赖信息文件:qrcode-utils-1.1.pom; 包含翻译后的API文档:qrcode-utils-1.1-javadoc-API文档-中文(简体)-...
此文章用来解释vue-cli脚手架build目录中的utils.js配置文件 1.此配置文件是vue开发环境的wepack相关配置文件,主要用来处理css-loader和vue-style-loader 2.关于注释 •当涉及到较复杂的解释我将通过标识的方式...
nfs离线安装包,也可通过离线命令下载到本地目录: yum -y install --downloadonly --downloaddir /root/nfs nfs-utils
赠送原API文档:hk2-utils-2.4.0-b34-javadoc.jar; 赠送源代码:hk2-utils-2.4.0-b34-sources.jar; 赠送Maven依赖信息文件:hk2-utils-2.4.0-b34.pom; 包含翻译后的API文档:hk2-utils-2.4.0-b34-javadoc-API文档...
AnyFo - Utils - XmlManager XmlManager 介绍 Xmlmanager本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作。
前端项目-jszip-utils,与JSZIP一起使用的跨浏览器实用程序集合。
赠送原API文档:hk2-utils-2.4.0-b34-javadoc.jar; 赠送源代码:hk2-utils-2.4.0-b34-sources.jar; 赠送Maven依赖信息文件:hk2-utils-2.4.0-b34.pom; 包含翻译后的API文档:hk2-utils-2.4.0-b34-javadoc-API文档...
注意:在unbuntu上按照压缩文档中的《sg3_utils测试工具交叉编译,静态编译.pdf》进行操作,可自行编译sg3_utils arm64 静态包 sg3_utils ARM64 静态编译链接程序,可直接在arm64平台上运行,用于对scsi 设备进行...
exfat 文件系统linux格式化代码,非常好用,直接交叉编译,就能使用,快速格式化代码
zip_utils_src c++关于压缩成zip文件的源码与实例!
Ti3559开发板asc主板文件uart文件等应用文件。utils
DevExpress.Utils3.DLL文件
资源文件中的路径解决问题 //鑾峰彇璧勬簮鏂囦欢鐨勮矾寰? String filepath = ContentMessage.class.getClassLoader().getResource("") .getPath(); filepath = filepath.substring(0, filepath.length()...
sourceInsight4.0.85版本的自定义命令,该配置文件替换后课可以使用InsertFileHeader,InsertHeader,InsertFunctionHeader对函数,以及创建的文件进行注释。提高开发项目的速度
uni-app 自己封装的utils.js 常用工具类(封装的ajax,上传,查看文档,富文本解析)
bridge-utils-1.4,linux系统上将多个网卡桥接的程序,还有帮助文档
utils.em Source Insight 原版文件,简单应用,不小心被删除可以使用此代码,如果sourceinsight不能使用可以参考此文件
包括字符串处理、类型转换、日期处理、加密解密、文件操作、读写cookie、分页、http类处理、路径处理、过滤特殊字符、SQL安全处理、HTML标签处理等帮助类文档