JavaWeb(6)之响应对象HttpServletResponse

JavaWeb(6)之响应对象HttpServletResponse

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

输出响应内容

package com.zze.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置输出 ContentType
        response.setContentType("text/plain;charset=utf-8");
        // 设置响应状态码
        response.setStatus(200);
        // 以字符流方式响应数据
        response.getWriter().write("hello");
        // 以字节流方式响应数据
        response.getOutputStream().write("world".getBytes());
    }
}

解决响应中文乱码

package com.zze.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置输出 ContentType,让浏览器以 utf-8 解析响应内容
        response.setContentType("text/plain;charset=utf-8");
        // 以 utf-8 编码响应内容
        response.setCharacterEncoding("utf-8");
        // 以字符流方式响应数据
        response.getWriter().write("哈哈哈哈哈");
        // java.lang.String.getBytes() 默认使用 utf-8 编码
        response.getOutputStream().write("啦啦啦啦啦".getBytes("utf-8"));
    }
}

响应流下载文件

package com.zze.servlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 文件名
        String fileName = "testdownload.cs";
        // 文件路径
        String filePath = "files/" + fileName;
        // 设置响应头通知浏览器下载文件而不是直接打开
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 获取该文件输入流
        // String realPath = getServletContext().getRealPath(filePath);
        // InputStream inputStream = new FileInputStream(realPath);
        InputStream inputStream = getServletContext().getResourceAsStream(filePath);
        // 转化为输出流响应
        ServletOutputStream outputStream = response.getOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inputStream.close();
    }
}

下载文件时文件名乱码的解决:

public static String EncodeFileName(HttpServletRequest request, String fileName) {
    try {
        String downloadFileName = null;
        String agent = (String) request.getHeader("USER-AGENT");
        if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
            downloadFileName = "=?UTF-8?B?" + (new String(Base64.getEncoder().encode(fileName.getBytes("UTF-8")))) + "?=";
        } else {
            downloadFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        }
        return downloadFileName;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/javaweb6.html

Buy me a cup of coffee ☕.