response练习1

1.使用response处理中文乱码
 
package com.hbsi.response;
 
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
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 Response1 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 
test5(response);
 
}
 
// 没有乱码,个体Bytes()默认是GB2312,浏览器默认也是GB2312
 private void test1(HttpServletResponse response) throws IOException,
 UnsupportedEncodingException {
 String str = "河北";
 
OutputStream out = response.getOutputStream();
 out.write(str.getBytes());
 }
 
// 有乱码,因此要进行处理
 private void test2(HttpServletResponse response) throws IOException,
 UnsupportedEncodingException {
 String str = "河北";
 // 发送消息头,通知浏览器以UTF-8打开内容
 // response.setHeader("Content-Type","text/html;charset=UTF-8");
 response.setContentType("text/html;charset=UTF-8");
 OutputStream out = response.getOutputStream();
 out.write(str.getBytes("UTF-8"));
 }
 
// 不发送http头,而是发送了一段html内容来控制浏览器以utf-8打开。
 private void test3(HttpServletResponse response) throws IOException,
 UnsupportedEncodingException {
 String str = "河北";
 // 发送消息头,通知浏览器以UTF-8打开内容
 
OutputStream out = response.getOutputStream();
 out
 .write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>"
 .getBytes());
 out.write(str.getBytes("UTF-8"));
 }
 
// 采用字符输出流,response默认的编码是iso8859-1,
 private void test4(HttpServletResponse response) throws IOException,
 UnsupportedEncodingException {
 String str = "河北";
 // 把response的编码改为UTF-8;
 // response.setCharacterEncoding("UTF-8");
 
// 告诉浏览器,以UTF-8显示
 // response.setHeader("Content-Type","text/html;charset=UTF-8");
 
response.setContentType("text/html;charset=UTF-8");
 PrintWriter out = response.getWriter();
 out.println(str);
 
}
 //课后思考!!
 private void test5(HttpServletResponse response) throws IOException,
 UnsupportedEncodingException {
 //response.getWriter().write(14);
 response.getOutputStream().write(14);
 
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 
doGet(request, response);
 }
 
}

 
2.使用response实现文件下载
 
package com.hbsi.response;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class Response2 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 
//1.获取要下载的资源
 String path = this.getServletContext().getRealPath("/download/视频1.avi");
 String filename = path.substring(path.lastIndexOf("\\")+1);
 //System.out.println(filename);
 
//2.通知浏览器以下载方式打开发送过来的数据
 //response.setHeader("content-disposition","attachment;filename="+filename);
 //如果文件名是中文,要经过URL编码
 response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
 //如果文件名是中文,要经过URL编码
 //3.读取资源内容
 FileInputStream fis = new FileInputStream(path);
 byte[] buffer = new byte[1024];
 int len = 0;
 while((len=fis.read(buffer))>0){
 response.getOutputStream().write(buffer,0,len);
 }
 fis.close();
 
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 
doGet(request, response);
 }
 
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值