一、前言
关于通过HttpServlet的HttpServletResponse输出response对象,写出HTML/XML/JSON/JAR/ZIP/XSL/JAR/JPG图片各种内容类型(application/json、text/html、image/jpeg、application/octet-stream等)接口文件数据源码示例,详情示例代码。
二、示例代码
package com.xwood.demo.gw;@b@@b@import java.awt.Color;@b@import java.awt.Graphics;@b@import java.awt.image.BufferedImage;@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.PrintWriter;@b@@b@import javax.imageio.ImageIO;@b@import javax.servlet.ServletException;@b@import javax.servlet.http.HttpServlet;@b@import javax.servlet.http.HttpServletRequest;@b@import javax.servlet.http.HttpServletResponse;@b@@b@public class RWFilesServlet extends HttpServlet {@b@ @b@ private static final long serialVersionUID = 1L;@b@ @b@ public RWFilesServlet() {}@b@ @b@ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {@b@ @b@ String fileName=request.getParameter("fileName");@b@ @b@ if(fileName.contains("json")){@b@ PrintWriter writer = response.getWriter();@b@// response.setCharacterEncoding("utf-8");@b@ response.setContentType("application/json;charset=utf-8"); @b@ writer.write("{\"fileType\":\"application/json\"}");@b@ writer.flush();@b@ writer.close();@b@ }else if(fileName.contains("html")){@b@ PrintWriter writer = response.getWriter();@b@ response.setContentType("text/html;charset=utf-8"); @b@ writer.write("<html><body><b>fileType</b>:text/html</body></html>");@b@ writer.flush();@b@ writer.close();@b@ }else if(fileName.contains("xml")){@b@ PrintWriter writer = response.getWriter();@b@ response.setContentType("text/xml;charset=utf-8"); @b@ writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><contentType>text/xml</contentType></root>");@b@ writer.flush();@b@ writer.close();@b@ }else if(fileName.contains("jpg")){@b@ OutputStream outputstream = response.getOutputStream();@b@ response.setContentType("image/jpeg"); @b@ BufferedImage bi = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);@b@ Graphics g = bi.getGraphics();@b@ g.setColor(Color.WHITE);@b@ ImageIO.write(bi, "jpg",outputstream);@b@ outputstream.close();@b@ }else{@b@ OutputStream outputStream = response.getOutputStream();@b@ response.setContentType("application/octet-stream; charset=utf-8");@b@ response.setHeader("Content-Disposition", "attachment; filename="+fileName);@b@ @b@ outputStream = new BufferedOutputStream(response.getOutputStream());//获取OutputStream输出流@b@ outputStream.write(getLocFileByte("c:/abdera-bundle-1.0.jar"));@b@ outputStream.write(getLocFileByte("c:/dataReport726.xls"));@b@ outputStream.write(getLocFileByte("c:/dataReport726.zip"));@b@ @b@ outputStream.flush();@b@ outputStream.close();@b@ }@b@ @b@ }@b@ @b@ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {@b@ doGet(request, response);@b@ }@b@ @b@ /**@b@ * 读取本地自定文件为byte数组@b@ * @param absPathFile 指定文件路径如c:/dataReport726.zip@b@ * @return@b@ */@b@ private byte[] getLocFileByte(String absPathFile){@b@ byte[] returnFileByte=null;@b@ try {@b@ File readFile = new File(absPathFile); // 要下载的文件绝对路径@b@ InputStream readFileInputStream = new BufferedInputStream(new FileInputStream(readFile));@b@ returnFileByte= new byte[readFileInputStream.available()];@b@ readFileInputStream.read(returnFileByte);@b@ readFileInputStream.close();@b@ } catch (Exception e) {@b@ }@b@ return returnFileByte;@b@ }@b@@b@ @b@}
Content-Type内容类型说明如下
1.常见的媒体格式类型如下:@b@ text/html : HTML格式@b@ text/plain :纯文本格式 @b@ text/xml : XML格式@b@ image/gif :gif图片格式 @b@ image/jpeg :jpg图片格式 @b@ image/png:png图片格式@b@ @b@2.以application开头的媒体格式类型:@b@ application/xhtml+xml :XHTML格式@b@ application/xml : XML数据格式@b@ application/atom+xml :Atom XML聚合格式 @b@ application/json : JSON数据格式@b@ application/pdf :pdf格式 @b@ application/msword : Word文档格式@b@ application/octet-stream : 二进制流数据(如常见的文件下载)@b@ application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)@b@ @b@3.上传文件媒体格式@b@ multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式