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);
 }
 
}

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Python 异步回调的练习题,希望对你有帮助: 假设你要编写一个程序,从一个 URL 列表中下载所有的网页内容,并将下载完成的网页内容保存到本地文件中。请使用 Python 异步回调实现这个程序。 具体实现步骤如下: 1. 定义一个 download_page 函数,该函数接收一个 URL 参数,并使用 requests 库下载该 URL 的网页内容,将网页内容传递给回调函数。 2. 定义一个 save_page 函数,该函数接收一个网页内容参数和一个文件名参数,并将网页内容保存到本地文件中。 3. 定义一个异步回调函数 callback,该函数接收两个参数,分别是下载完成的网页内容和要保存的文件名。该函数将调用 save_page 函数将网页内容保存到本地文件中。 4. 定义一个 main 函数,该函数接收一个 URL 列表参数。该函数使用 asyncio 库创建一个事件循环,然后遍历 URL 列表,每个 URL 调用 download_page 函数下载网页内容,并将回调函数 callback 和要保存的文件名作为参数传递给 download_page 函数。最后,运行事件循环让程序异步执行。 以下是代码示例: ```python import asyncio import requests async def download_page(url, callback, filename): response = requests.get(url) content = response.content callback(content, filename) def save_page(content, filename): with open(filename, 'wb') as f: f.write(content) def callback(content, filename): asyncio.create_task(download_page(url, save_page, filename)) async def main(urls): tasks = [] for i, url in enumerate(urls): filename = f'page-{i}.html' task = asyncio.create_task(download_page(url, callback, filename)) tasks.append(task) await asyncio.gather(*tasks) if __name__ == '__main__': urls = [ 'https://www.baidu.com', 'https://www.google.com', 'https://www.bing.com', ] asyncio.run(main(urls)) ``` 在上述代码中,download_page 函数使用 requests 库下载网页内容,将下载完成的网页内容传递给回调函数 callback。callback 函数将运行在事件循环中,使用 asyncio.create_task 创建一个新的任务,该任务调用 download_page 函数下载网页内容,并将回调函数 save_page 和要保存的文件名作为参数传递给 download_page 函数。save_page 函数将网页内容保存到本地文件中。 main 函数使用 asyncio 库创建一个事件循环,遍历 URL 列表,每个 URL 调用 download_page 函数下载网页内容,并将回调函数 callback 和要保存的文件名作为参数传递给 download_page 函数。最后,使用 asyncio.gather 方法等待所有任务完成,程序异步执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值