文件下载中文乱码问题解决方案

版权声明:尊重博主原创文章,转载请注明出处。如有不妥之处,望各位指正。联系方式:[email protected] https://blog.csdn.net/Supreme_Sir/article/details/88994581

开发过程中(web 服务器为 Tomcat7)我们难免会遇到文件下载的功能,可随之而来的就是当下载名称包含中文的文件时的乱码问题,今天博主在此记录一下遇到该问题的解决方案。(如果您的 web 服务器为 Tomcat8 请查看 Java代码下载功能在 Tomcat7、8 不同环境中,中文乱码问题

web.xml

<!-- 配置文件下载功能文件下载路径 -->
<context-param>
	<param-name>commonFile</param-name>
	<param-value>E:\</param-value>
</context-param>

DownloadUtil.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/download")
public class DownloadUtil {

	// 方式一
	@RequestMapping("/commonFile")
	public void downloadReport(@Param("fileName") String fileName, HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (fileName != null && !fileName.equals("")) {
				// 转换文件名称编码格式
				String file = new String(fileName.getBytes("ISO8859-1"), "utf-8");
				// 设置响应头
				resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
				// 设置响应内容类型
				resp.setContentType(req.getServletContext().getMimeType(file));
				// 获取配置在 web.xml 中的文件路径
				String filePath = (String) req.getServletContext().getInitParameter("commonFile");
				// 获取文件流
				InputStream fis = new BufferedInputStream(new FileInputStream(filePath + "/" + file));
				byte[] buffer = new byte[fis.available()];
				fis.read(buffer);
				// 输出文件流
				OutputStream toClient = new BufferedOutputStream(resp.getOutputStream());
				toClient.write(buffer);
				// 关闭文件流
				toClient.flush();
				fis.close();
				toClient.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// ☆ 推荐:方式二
	@RequestMapping("/downloadFile")
	public ResponseEntity<byte[]> downloadCommonFile(@Param("fileName") String fileName, HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (StringUtils.isNoneBlank(fileName)) {
				// 转换文件名称编码格式
				String file = new String(fileName.getBytes("ISO-8859-1"), "utf-8");
				// 获取配置在 web.xml 中的文件路径
				String filePath = (String) req.getServletContext().getInitParameter("commonFile");
				// 设置响应头
				HttpHeaders headers = new HttpHeaders();
				headers.setContentDispositionFormData("attachment", fileName);
				// 设置响应内容类型
				headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
				// 判断文件是否存在
				File downloadFile = new File(filePath + "/" + file);
				if(downloadFile.exists()){
					// 返回文件流
					return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(downloadFile), headers, HttpStatus.CREATED);
				}else{
					// 抛出文件未找到异常
					throw new FileNotFoundException();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}  

以上便是博主应对文件下载功能时中文乱码问题的解决方案,如有不懂敬请留言。

猜你喜欢

转载自blog.csdn.net/Supreme_Sir/article/details/88994581