[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
web.xml
Content-Disposition首部域指示客户端将接收的数据作为附件,而不是作为字符显示在浏览器中。
|文件|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首部域指示客户端将接收的数据作为附件,而不是作为字符显示在浏览器中。