Servlet上传文件解决中文乱码问题


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
         
		
        <servlet>
            <servlet-name>UploadServletservlet-name>
            <servlet-class>ProjectTest.UploadServletservlet-class>
        servlet>
        <servlet-mapping>
            <servlet-name>UploadServletservlet-name>
            <url-pattern>/uploadServleturl-pattern>
        servlet-mapping>
web-app>
<%--
  Created by IntelliJ IDEA.
  User: 87672
  Date: 2021/1/9
  Time: 20:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传title>
head>
<body>
	
    <form action="http://localhost:8080/_1_9File_war_exploded/uploadServlet" method="post" enctype="multipart/form-data">
        用户名:<input type="text" name="username"><br>
        头像:<input type="file" name="photo"><br>
        <input type="submit" value="上传">
    form>
body>
html>

Servlet上传文件解决中文乱码问题_第1张图片

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

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

//上传文件的程序
public class UploadServlet extends HttpServlet {
     
    /**
     * Java上传文件
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
        //1.先判断上传的数据是否是多段数据(只有多段数据才是文件上传的)
        if(ServletFileUpload.isMultipartContent(req)){
     
            //创建FileItemFactory工厂实现类
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //创建用于解析上传数据的工具类ServletFileUpload类
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            //解析上传的数据,得到每一个表单项FileItem
            try {
     
                List<FileItem> list = servletFileUpload.parseRequest(req);
                //循环判断每一个表单项是普通类型还是上传的文件
                for (FileItem fileItem : list) {
     
                    if(fileItem.isFormField()){
     
                        //普通表单项
                        System.out.println("表单项的name属性值:" + fileItem.getFieldName());
                        //参数UTF-8解决乱码问题
                        System.out.println("表单项的value属性值:" + fileItem.getString("UTF-8"));
                    }else {
     
                        //上传的文件
                        System.out.println("表单项的name属性值:" + fileItem.getFieldName());
                        System.out.println("上传的文件名:" + fileItem.getName());

                        fileItem.write(new File("e:\\" + fileItem.getName()));
                    }
                }
            } catch (Exception e) {
     
                e.printStackTrace();
            }
        }
    }
}

问题:从Java程序中可以看出,已经设置编码字符集为UTF-8,但文件名若包含中文,依旧会出现中文乱码,若下图所示
Servlet上传文件解决中文乱码问题_第2张图片在这里插入图片描述
解决方案:必须加上如下代码
servletFileUpload.setHeaderEncoding(“UTF-8”);如下图所示
Servlet上传文件解决中文乱码问题_第3张图片
在这里插入图片描述

你可能感兴趣的:(Java,servlet,java,乱码)