Servlet发送pdf文件

[table]
|文件|MIME类型|扩展名
|XML|text/xml|.xml
|HTML|text/html|.html
|纯文本|text/plain|.txt
|PDF|application/pdf|.pdf
|GIF图片|image/gif|.gif
|JPEG|image/jpeg|.jpeg
|PNG|image/x-png|.png
|mp3|audio/mpeg|.mp3
|ShockwaveFlash动画|application/futuresplashaa或application/x-shockwave-flash|.swf
|word|application/msword|.doc
|Excel|application/vnd.ms-excel|.xls
|PowerPoint|application/vnd.ms-powerpoint|.ppt
[/table]

发送pdf文件
访问URL为http://localhost:8080/success/sendpdf?file=filename


package myServlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class sendpdf extends HttpServlet {

/**
* Constructor of the object.
*/
public sendpdf() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String filename=(String)request.getParameter("file");
if(filename==""||filename==null)
{
throw new ServletException("invalid filename");
}
if(filename.indexOf(".pdf")==-1)
{
filename=filename+".pdf";
}

//pdf保存在何处
String pdfDir=getServletContext().getInitParameter("pdf_dir");
if(pdfDir.equals("")||pdfDir==null)
{
throw new ServletException("invlid pdfDir");
}

ServletOutputStream stream=null;
BufferedInputStream input=null;

try
{
stream=response.getOutputStream();
File file=new File(pdfDir+"/"+filename);

response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename="+filename);

response.setContentLength((int) file.length());

FileInputStream finput=new FileInputStream(file);
input=new BufferedInputStream(finput);
int readBytes=0;
while((readBytes=input.read())!=-1)
{
stream.write(readBytes);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
if(stream!=null)
{
stream.close();
}
if(input!=null)
{
input.close();
}
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request,response);
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}




web.xml

<context-param>
<param-name>pdf_dir</param-name>
<param-value>e:/test</param-value>
</context-param>


Content-Disposition首部域指示客户端将接收的数据作为附件,而不是作为字符显示在浏览器中。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
servlet课件Servlet课件..........................................................................................1 第一章 Servlet基础部分 .....................................................................2 1.1 Servlet简介 .........................................................................2 1.2 Web服务器...........................................................................2 1、介绍...............................................................................2 2、Tomcat的配置..................................................................2 3、Tomcat服务器具体介绍 .......................................................3 4、jar命令介绍......................................................................3 1.3 Servlet的配置文件 .................................................................3 1.4 Servlet的调用过程 .................................................................4 第二章 Servlet核心...........................................................................5 2.1 整体介绍 .............................................................................5 2.2 Servlet的接口.......................................................................5 2.3 servlet的生命周期..................................................................6 1、Servlet的生命周期分为四个阶段 ............................................6 2、在web.xml文件中配置Servlet对象的启动时就创建......................7 3、通过<init-param>标签来配置初始化参数.................................7 2.4 Get请求和Post请求 ................................................................7 2.5 ServletRequest ....................................................................8 request.getInputStream()方法................................................8 request.getReader()方法 .......................................................8 request.getParameter(String name)方法,...............................8 request.getContextPath()方法 ................................................8 等等。。。。。。。。。。。。。。。。。。。。
Servlet3.1规范(最终版) JavaTM Servlet 规范 版本 3.1(最终版) Shing Wai Chan Rajiv Mordani [作者] 穆茂强 张开涛 [译者] 2012年6月翻译 2013年7月修订 目录 前言 ............................... 3 其他资料 ............................................................................................................................................................ 3 谁应该读此规范 ................................................................................................................................................ 3 API规范 ............................................................................................................................................................3 其他的 Java 平台规范 ....................................................................................................................................... 3 其他重要参考资料 ............................................................................................................................................ 4 提供反馈 ............................................................................................................................................................ 4 专家组成员 ........................................................................................................................................................ 5 答谢 .................................................................................................................................................................... 5 目录 ............................... 6 概览 .............................. 13 1.1 1.2 1.3 1.4 1.5 1.6 什么是 Servlet? .................................................................................................................................. 13 什么是Servlet容器?...............................................................................................................................13 例子...........................................................................................................................................................13 Serv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值