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

涉及到的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<Song> selectList() {
        return songMapper.selectList(null);
    }

    @Override
    public Page<Song> selectByPage(SongPagerDTO songPagerDTO) {
        QueryWrapper<Song> wrapper = Pager.getQueryWrapper(songPagerDTO, true);
        Page<Song> 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<Song> 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<Void> 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);
    }

}

 

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

 

文件的内容也是正常的

 

后台也没报错

 

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

 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值