HttpServletResponse相关

目录

返回给客户端数据:字节流和字符流

解决返回字节流和字符流乱码问题

万能解决乱码

文件下载

tomcat提供的默认下载方式

手动编写的DownloadServlet


返回给客户端数据:字节流和字符流

package com.zhujunwei.httpServletResponse;

import java.io.IOException;

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

public class HttpServletResponse01 extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		/**
		 * 返回给客户端数据两种方式:字节流和字符流方式
		 * 一次只能执行其中的一个
		 */
//		byWriter(resp);
		byOutputStream(resp);
		
		//设置当前这个请求的处理状态码
//		resp.setStatus(sc);
		
		//设置一个头
//		resp.setHeader(name, value);
		
		//设置相应的类型,以及编码
//		resp.setContentType(type);
	}

	/**
	 * 以字节流的方式写数据
	 * @param resp
	 * @throws IOException
	 */
	private void byOutputStream(HttpServletResponse resp) throws IOException {
		resp.getOutputStream().write("getOutputStream().write()".getBytes());
	}

	/**
	 * 以字符流的方式写数据
	 * @param resp
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	private void byWriter(HttpServletResponse resp) throws IOException {
		resp.getWriter().write("<h1>getWriter().write()</h1><br>");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

结果:

getOutputStream().write()

解决返回字节流和字符流乱码问题

package com.zhujunwei.httpServletResponse;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

public class HttpServletResponse02 extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//字符流输出
//		writerEncoding(resp);
		//字节流输出
		outPutStreamEncoding(resp);
		
	}
	
	/**
	 * 解决字节流输出乱码
	 * @param resp
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	private void outPutStreamEncoding(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
		//1、指定浏览器看这份数据使用的码表
		resp.setHeader("Content-Type", "text/html; charset=UTF-8");
		//2、指定输出的中文用的码表
		resp.getOutputStream().write("我是最帅的。。".getBytes());
	}
	/**
	 * 解决字符流输出乱码
	 * @param resp
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	private void writerEncoding(HttpServletResponse resp) throws IOException {
		//这里写出去的文字,默认使用的是ISO-8859-1,我们可以指定写出去的时候,使用什么编码写
		//1、指定输出到客户端的时候,这些文字使用UTF-8
		resp.setCharacterEncoding("UTF-8");
		//2、直接规定浏览器看这份数据的时候,使用什么编码看
		resp.setHeader("Content-Type", "text/html; charset=UTF-8");
		
		resp.getWriter().write("我是最帅的。。。");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

结果:

我是最帅的。。

万能解决乱码

package com.zhujunwei.httpServletResponse;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

public class HttpServletResponse03 extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//只需要设置这一行就可以不管是字节或者字符流都不会乱码
		resp.setContentType("text/html; charset=UTF-8");
		//字符流输出
		writerEncoding(resp);
		//字节流输出
//		outPutStreamEncoding(resp);
		
	}
	
	/**
	 * 解决字节流输出乱码
	 * @param resp
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("unused")
	private void outPutStreamEncoding(HttpServletResponse resp) throws IOException, UnsupportedEncodingException {
		resp.getOutputStream().write("我真的是最帅的。。".getBytes());
	}
	/**
	 * 解决字符流输出乱码
	 * @param resp
	 * @throws IOException
	 */
	private void writerEncoding(HttpServletResponse resp) throws IOException {
		resp.getWriter().write("我真的是最帅的。。。");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

结果

我真的是最帅的。。。

文件下载

tomcat提供的默认下载方式

Tomcat提供的有文件下在的默认Servlet,如果浏览器能打开这种文件的话会自动打开,不会弹出下载框。

我们在项目里面放置几个文件供测试使用

download.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>tomcat提供的默认下载方式</h2>
	<a href="download/test.txt">test.txt</a><br>
	<a href="download/m1.jpg">m1.jpg</a><br>
	<a href="download/download.rar">download.rar</a><br>
	<hr>
	<h2>手动写下载方法</h2>
	<a href="DownloadServlet?fileName=test.txt">test.txt</a><br>
	<a href="DownloadServlet?fileName=m1.jpg">m1.jpg</a><br>
	<a href="DownloadServlet?fileName=download.rar">download.rar</a><br>
	 
</body>
</html>

手动编写的DownloadServlet

package com.zhujunwei.download;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

public class DownloadServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//1、获取要下载的文件名字
		String fileName = req.getParameter("fileName");
		
		//2、获取这个文件在tomcat里面的绝对路径地址
		String path = getServletContext().getRealPath("download/"+fileName);
		//让浏览器收到这份资源的时候,以下载的方式提醒用户,而不是直接展示
		resp.setHeader("Content-Disposition", "attachment; filename="+fileName);
		
		//3、转换成输入流
		InputStream is = new FileInputStream(path);
		OutputStream os = resp.getOutputStream();
		
		int len = 0 ;
		byte[] buffer = new byte[1024];
		while((len = is.read(buffer))!=-1) {
			os.write(buffer,0,len);
		}
		os.close();
		is.close();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

结果:

 

 

posted @ 2018-12-28 00:13  雨中遐想  阅读(4)  评论(0编辑  收藏  举报  来源