侧边栏壁纸
博主头像
张种恩博主等级

一个能运维的 JPG 搬运工

  • 累计撰写 709 篇文章
  • 累计创建 62 个标签
  • 累计收到 27 条评论

目 录CONTENT

文章目录

JavaWeb(6)之响应对象HttpServletResponse

张种恩
2017-06-08 / 0 评论 / 0 点赞 / 442 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2020-04-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

输出响应内容

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

评论区