下载文件时的文件名中文乱码问题,文件名丢失

涉及到的java代码如下,下载的时候文件名为中文

package com.example.springboot.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.base.Pager;
import com.example.springboot.dto.SongInsertDTO;
import com.example.springboot.dto.SongPagerDTO;
import com.example.springboot.dto.SongUpdateDTO;
import com.example.springboot.entity.Song;
import com.example.springboot.mapper.SongMapper;
import com.example.springboot.restful.JsonResult;
import com.example.springboot.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SongServiceImpl implements SongService {

    private final SongMapper songMapper;

    @Autowired
    public SongServiceImpl(SongMapper songMapper) {
        this.songMapper = songMapper;
    }

    @Override
    public void insert(SongInsertDTO insertDTO) {
        Song song = new Song();

        song.setId(uuid());
        song.setName(insertDTO.getName());
        song.setSinger(insertDTO.getSinger());

        if (StringUtils.isNotEmpty(insertDTO.getNote())) {
            song.setNote(insertDTO.getNote());
        }

        songMapper.insert(song);
    }

    @Override
    public void deleteById(String id) {
        songMapper.deleteById(id);
    }

    @Override
    public void updateById(SongUpdateDTO updateDTO) {
        Song song = new Song();

        song.setId(updateDTO.getId());
        song.setName(updateDTO.getName());
        song.setSinger(updateDTO.getSinger());

        if (StringUtils.isNotEmpty(updateDTO.getNote())) {
            song.setNote(updateDTO.getNote());
        }
        song.setLastUpdateTime(LocalDateTime.now());

        songMapper.updateById(song);
    }

    @Override
    public Song selectById(String id) {
        return songMapper.selectById(id);
    }

    @Override
    public List selectList() {
        return songMapper.selectList(null);
    }

    @Override
    public Page selectByPage(SongPagerDTO songPagerDTO) {
        QueryWrapper wrapper = Pager.getQueryWrapper(songPagerDTO, true);
        Page page = Pager.ofPage(songPagerDTO);

        return songMapper.selectPage(page, wrapper);
    }

    @Override
    public void export(HttpServletResponse response) {
        String fileName = "歌曲列表.xlsx";

        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        try {
            List songs = songMapper.selectList(null);

            EasyExcel.write(response.getOutputStream(), Song.class).sheet("歌曲列表").doWrite(songs);
        } catch (Exception e) {
            e.printStackTrace();

            response.reset();
            response.setContentType("application/json;charset=utf-8");
            JsonResult jsonResult = JsonResult.success("数据导出异常");

            try {
                response.getWriter().write(jsonResult.toString());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    /**
     * 根据当前时间生成UUID
     * @return String
     */
    private static String uuid() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDate = LocalDateTime.now();

        return localDate.format(formatter);
    }

}

 

然后下载很快,但是文件名没有了

下载文件时的文件名中文乱码问题,文件名丢失_第1张图片

 

文件的内容也是正常的

下载文件时的文件名中文乱码问题,文件名丢失_第2张图片

 

后台也没报错

下载文件时的文件名中文乱码问题,文件名丢失_第3张图片

 

一般这种情况下可能会无从下手,F12打开控制台看一下,发现后端设置的响应头Content-disposition的文件名乱码了!

下载文件时的文件名中文乱码问题,文件名丢失_第4张图片

 

最终解决方案:使用utf-8格式对文件名进行编码

String fileName = URLEncoder.encode("歌曲列表.xlsx", "utf-8");

你可能感兴趣的:(开发中遇到的问题,windows,服务器,linux)