package cn.terje.web;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 针对不同的格式发送不同的文件格式到 client
*
* */
public class DocShowServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String fN = req.getParameter( "fileName" );
if (fN == null )
{
resp.sendError(404); // 出错了
} else
{
String fileName = "F:\\DOC\\" + fN; // 资料存放在 E DOC 下,主要是在 WIN 平台下
// 先判断文件类型,根据不同的文件类型选择不同的发送方式
if (Pattern.matches ( ".+\\.(html)" , fN))
{
resp.setContentType( "text/html" );
resp.setCharacterEncoding( "gb2312" );
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream( new File(fileName))); // 读取文件
byte [] b = new byte [1024];
int len;
while ((len = bis.read(b)) != -1)
{
out.write(b);
}
bis.close();
}
// 将 PDF 发送到 client
if (Pattern.matches ( ".+\\.(pdf)" , fN))
{
resp.setContentType( "application/pdf" );
resp.setCharacterEncoding( "gb2312" );
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream( new File(fileName)));
byte [] b = new byte [1024];
int len;
while ((len = bis.read(b)) != -1)
{
out.write(b);
}
bis.close();
}
// 发送 word 文档
if (Pattern.matches ( ".+\\.(doc)" , fN))
{
resp.setContentType( "application/msword" );
resp.setCharacterEncoding( "gb2312" );
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream( new File(fileName)));
byte [] b = new byte [1024];
int len;
while ((len = bis.read(b)) != -1)
{
out.write(b);
}
bis.close();
}
// 电子表格发送到 client
if (Pattern.matches ( ".+\\.(xls)" , fN))
{
resp.setContentType( "application/vnd.ms-excel" );
resp.setCharacterEncoding( "gb2312" );
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream( new File(fileName)));
byte [] b = new byte [1024];
int len;
while ((len = bis.read(b)) != -1)
{
out.write(b);
}
bis.close();
}
//PPT 发送到 client
if (Pattern.matches ( ".+\\.(ppt)" , fN))
{
resp.setContentType( ">application/vnd.ms-powerpoint" );
resp.setCharacterEncoding( "gb2312" );
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream( new File(fileName)));
byte [] b = new byte [1024];
int len;
while ((len = bis.read(b)) != -1)
{
out.write(b);
}
bis.close();
}
}
}
}
分享到:
相关推荐
用于ubuntu 16.04系统的mongodb客户端,亲测可以用的。
FastDFS服务器是一个轻量级的文件存储服务,结合Nginx使用,实现集群高可用,但是他的java客户端不是特别好找。本java客户端依赖包,能够完美整合实现FastDFS服务器的连接,上传、下载和删除。 内部附有简单API使用...
利用完成端口(IOCP)实现的在线网络考试系统的客户端程序。
Microsoft Office Word 2007 For Dummies.pdf Microsoft Sharepoint 2007 For Dummies.chm Microsoft SQL Server 2005 Express Ed For Dummies.pdf Microsoft SQL Server 2005 For Dummies.pdf Microsoft SQL Server...
Sockets and the socket API are used to send messages across a network. They provide a form of [inter-process communication (IPC)](https://en.wikipedia.org/wiki/Inter-process_communication). The ...
我只想说我是50分下载的,而且自己安装成功了,也已经成功连接了Oracle数据库,好的东西 何必那么高的分给人下在,而且我找一个上午才找到这个资源。
使用于.NET开发Web开发的编辑器控件,功能强大的编辑器!
if(doc.layers[0].kind==LayerKind.TEXT)s=doc.layers[0].textItem.contents; var Alie=excel.Range("A"+s); var Blie=excel.Range("B"+s); var Clie=excel.Range("C"+s); alert(Alie+" "+Blie+" "+Clie)
SVN Windows下的客户端软件的说明,值得一看
rdr-客户端客户端用于在渲染视频。 修改源代码以用于官方 o!rdr 服务器将导致禁止。 NodeJS v14+ 是必需的。这有什么作用? o!rdr 是一个免费且易于使用的 API/网站,可让您渲染 osu! 使用 Danser 重播的视频 ( )。...
cas 客户端文件,采用此客户端可以实现cas单点登陆即sso,是有耶鲁大学开发
SuperSocket.ClientEngine.Core socket 客户端处理粘包半包,个人手撸代码
淘淘商城/宜立方商城等电商项目图片上传客户端,现在maven中央仓库是没有相关依赖包的,现在给大家提供源码包,大家安装到本地仓库就可以使用maven管理依赖包了
PLSQL64位,需要安装,压缩文件内包含密钥,Oralce客户端安装文件。
#狂热版 #####₪ 关于 Fanatic Edition 是一个实验性的客户端修改,由成员开发,可用于 Windows 和 Linux,并在下获得许可。 该项目本身目前处于alpha状态,每天都会出现错误/修复。... 在 linux 上,将faned_clie
SFTP client library functions that created using SecureBlackBox VCL and enchance with resume features for download and upload.
本机不安装Oracle客户端,使用PL/SQL Developer连接远程数据库
TCP\UDP网络开发编程,实现了UDP的客户端,只是实现了UDP通信原理,功能简单清晰,非常适合学习
Oauth2客户端 该库旨在简化对启用了Oauth2的REST服务的使用。 它包装了一个restclient,并在需要时负责重新认证过期的access_token。 流量 已实现的流程是: 客户证书授予 资源所有者密码凭证授予 例子 使用密码...
包redis以前是官方 redis C 客户端的包装器。 截至 2014 年 10 月,包redis被拆分为两个不同的包: ( resp ) 和本页描述的( redis )。 如何安装或升级 使用go get安装或升级 ( -u ) redis包。 go get -u ...