中国のゴミ問題の解決策をダウンロード

免責事項:ブロガーのオリジナル記事を尊重し、ソースを明記してください。間違っている場合は、私に希望を修正します。連絡先:[email protected] https://blog.csdn.net/Supreme_Sir/article/details/88994581

開発プロセス(webサーバーTomcat7、ファイルのダウンロードの名は中国のゴミ問題が含まれている場合、我々は必然的に、ファイルのダウンロード機能が発生しますが)、係員は、今日のブロガーは、問題の解決方法についての記録に出くわします。(ご使用の場合はweb、サーバーがTomcat8参照Tomcat7,8異なる環境でのJavaコードのダウンロード機能を、中国の文字化け

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