JavaEE7_文件上传中文乱码问题

本文探讨了在JavaEE7环境下,文件上传时遇到的中文乱码问题。作者通过实例展示了在Tomcat7和Tomcat8中GET和POST请求的不同乱码情况,并详细解释了解决乱码的方法,包括设置请求编码、使用`new String()`转换以及浏览器如何根据页面编码判断请求的字符集。
摘要由CSDN通过智能技术生成

文件上传中文乱码引发的问题

1.首先写这个博客,是因为在文件上传的时候出现了自己想不通的问题。开始列举问题

文件下载使用GET请求方式请求Servlet,当获取中文名称的图片时我了设置request编码但是还是乱码的,反而我不设置request编码中文名称不乱码。然后我写了一个servlet测试了一下。

请求servlet
在这里插入图片描述

代码演示

@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println(username);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp );
    }
}

结果:

张三

原因:是因为tomcat8已经解决GET提交方式乱码,POST方式还是要解决乱码。

2.然后把上面的servlet部署在tomcat7中,在获取username参数出现了乱码
结果:

??????

3.解决tomcat7GET提交方式乱码
在这里插入图片描述

@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        username =  new String(username.getBytes("iso8859-1"),"utf-8");
        System.out.println(username);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp );
    }
}

结果:

张三

4.为什么 username = new String(username.getBytes(“iso8859-1”),“utf-8”);能解决乱码
还有一点就是解码为什么是utf-8,为什么不是gbk?
其实当时有想过是因为浏览器编码是utf-8,所以这里解码是utf-8,其实的确是这样的。
但是浏览器怎么知道是utf-8,而不是gbk?浏览器是根据什么来判断编码的呢?

html页面
在这里插入图片描述

jsp页面
在这里插入图片描述
证明浏览器根据html或者jsp的contenttype编码来调整编码。
使用IE浏览器访问上面的JSP查看编码----Unicode(UTF-8)

将jsp中的contentType编码换成gbk编码

<%@ page contentType="text/html;charset=gbk" language="java" %>
<html>
  <head>
    <title>Login</title>
  </head>
  <body>
    <form action="/download/login" method="get">
        名字:<input type="text" >
    </form>
  </body>
</html>

在使用IE浏览器查看编码是----简体中文(GBK2312)

5.使用contentType换成gbk编码的jsp页面解决中文乱码
在这里插入图片描述

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        username = new String(username.getBytes("iso8859-1"),"gbk");
        System.out.println(username);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

结果:

张三

request乱码解码解决图解
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值