Day03【Response】设置响应体

response-设置响应体-getWriter

在这里插入图片描述

  • (1)设置响应体的方法
  • 1)getWriter()方法:字符流
    只能向浏览器响应文本内容: 字符串,标签
    为什么会产生中文乱码?Tomcat IOS 8859-1
  • 2)getOutputStream()方法:字节流
    可以向浏览器响应任何类型的数据: 图片,视频,音频
    用于开发下载功能
    src\com\wzx\pack04_body\Demo04Servlet.java
@WebServlet("/body")
public class Demo04Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1:字符流
        response.setHeader("Content-Type","text/html;charset=utf-8");
        response.getWriter().write("<font color='red'>中国</font>");
    }
}

response-设置响应体-getOutputstream

(1)字节输出流
a:如果向页面输出字符串,不需要解决乱码
b: 向页面输出字符串时,使用write

src\com\wzx\pack04_body\Demo05Servlet.java

写字符串一般使用字符流,但是必须要处理编码。而使用字节流能搞定字符串?能~且不需要处理编码,但是字节流真正的作用是写文件数据

@WebServlet("/body2")
public class Demo05Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取字节输出流
        ServletOutputStream outputStream = response.getOutputStream();
        //写数据到浏览器
        byte[] bytes = "中国".getBytes();
        outputStream.write(bytes);
		//关闭
        outputStream.close();
    }
}

response-设置响应体-图片的复制

在这里插入图片描述
test\java\com\wzx\service\TestFileService.java


public class TestFileService {
    @Test//1:导入junit
    public void test01() throws IOException {

        //文件复制

        String file1 = "a.jpg";
        String file2 = "b.jpg";

        InputStream inputStream =TestFileService.class.getClassLoader().getResourceAsStream("a.jpg");
        File file = new File(file2);
        file.createNewFile();

        OutputStream outputStream = new FileOutputStream("b.jpg");
        //1:创建文件业务类
        FileService fileService = new FileService();
        //2:调用业务方法copy
        fileService.copy(inputStream,outputStream);

        //3:关闭流
        inputStream.close();
        outputStream.close();

    }
}

src\com\wzx\service\FileService.java

//文件业务类
public class FileService {
    //复制文件
    public void copy(InputStream inputStream, OutputStream outputStream) throws IOException {
        //边读边写
        //1:字节数组 缓冲区
        byte[] buffer=new byte[1024];
        int len = 0;
        //2:边读边写
        while ((len=inputStream.read(buffer))!=-1){
            outputStream.write(buffer,0,len);
        }
    }
}

浏览器访问Servlet显示图片

本质 就是 将一个文件的所有字节,复制到浏览器
src\com\wzx\pack05_download\Demo05Servlet.java


@WebServlet("/download")
public class Demo05Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.加载a.jpg文件成为一个输入流
        InputStream inputStream =  getServletContext().getResourceAsStream("download/a.mp3");
        //2.再使用字节流 将数据写到浏览器
        OutputStream outputStream = response.getOutputStream();
        //3.浏览器自动将数据显示成图片
        FileService fileService = new FileService();
        fileService.copy(inputStream,outputStream);
        //4.关闭
        outputStream.close();
        inputStream.close();
    }
}

response-设置响应体-超链接访问文件

  • (1)超链接访问图片
    浏览器本身支持,本质也是先读文件,再将文件写到浏览器
    web\index.html
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="/web01_reponse/download/a.jpg">点一点看图片</a><br/>
    <a href="/web01_reponse/download/a.mp3">点一点看mp3</a><br/>
    <a href="/web01_reponse/download/a.mp4">点一点看mp4</a><br/>
    <a href="/web01_reponse/download/a.zip">点一点看zip</a><br/>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/108483889