Download Chinese garbage problem solution

Disclaimer: Respect bloggers original articles, please indicate the source. If wrong, correct me hope. Contact: [email protected] https://blog.csdn.net/Supreme_Sir/article/details/88994581

The development process ( webserver Tomcat7) we will inevitably encounter file download function, the attendant is when the file download name contains the Chinese garbage problem, today bloggers come across on the record about the solution to the problem . (If your webserver Tomcat8See the Java code download capability in Tomcat7,8 different environment, Chinese garbled )

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;
	}
}  

The above is when bloggers deal with file download function Chinese solution to the garbage problem, if do not understand please leave a message.

Guess you like

Origin blog.csdn.net/Supreme_Sir/article/details/88994581