涉及到的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;@Autowiredpublic SongServiceImpl(SongMapper songMapper) {this.songMapper = songMapper;}@Overridepublic 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);}@Overridepublic void deleteById(String id) {songMapper.deleteById(id);}@Overridepublic 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);}@Overridepublic Song selectById(String id) {return songMapper.selectById(id);}@Overridepublic List<Song> selectList() {return songMapper.selectList(null);}@Overridepublic Page<Song> selectByPage(SongPagerDTO songPagerDTO) {QueryWrapper<Song> wrapper = Pager.getQueryWrapper(songPagerDTO, true);Page<Song> page = Pager.ofPage(songPagerDTO);return songMapper.selectPage(page, wrapper);}@Overridepublic 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");