Spring MVC 学习笔记 5《5.3 文件下载》

直接上代码吧

package com.jerry.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
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;

import com.jerry.ssm.entity.FileInfo;
import com.jerry.ssm.service.IFileInfoService;
import com.jerry.ssm.util.PageObj;

@Controller
@Scope("prototype")
public class DownLoadController {
	@Resource
	private IFileInfoService fileInfoService;

	/**
	 * 下载文件页面 http://localhost/goDownloadFile.html?
	 */
	@RequestMapping("goDownloadFile.html")
	public String goDownloadFile(Map<String, Object> model) throws IOException {
		return "goDownloadFile";
	}

	/**
	 * 下载接口 http://localhost/download?id=1
	 * @param id 文件id
	 * @param resp
	 * @param req
	 * @throws IOException
	 */
	 @RequestMapping("download")
	public void downloadFile(Long id, HttpServletResponse resp, HttpServletRequest req) throws IOException {
		// 这些数据应该是通过某种方式从数据库取出来的比如id 。我图省事直接写了。
		String path = "D:\\upload_files\\file\\20190503\\"; // 获取资源存储路径
		String fileName = "1_20190503031553.rar"; // 存在服务器硬盘上的文件名
		String originalFilename = "test.rar";// 文件原始名称
		
		// 设置响应头:告诉浏览器将接受的流数据另存为存储到客户端,而不是直接解析
		// 设置响应的数据的MIME类型
		resp.setContentType("multipart/form-data");
		resp.setHeader("Content-Disposition", "attachment;filename=" + originalFilename );

		// 获取输出流。输出要下载的资源
		ServletOutputStream os = resp.getOutputStream();
		os.write(FileUtils.readFileToByteArray(new File(path, fileName )));
	}

	/**
	 * @Description 下载文件
	 * @param id 文件id
	 * @param response
	 */
	@RequestMapping("download2")
	public static void downLoadFile(Long id, HttpServletResponse response) {
		// 这些数据应该是通过某种方式从数据库取出来的比如id 。我图省事直接写了。
		String path = "D:\\upload_files\\file\\20190503\\"; // 获取资源存储路径
		String fileName = "1_20190503031553.rar"; // 存在服务器硬盘上的文件名
		String originalFilename = "test.rar";// 文件原始名称
		
		File file = new File(path , fileName );
		String originalFilename = "test.rar";// 文件原始名称
		if (file == null || !file.exists()) {
			return;
		}
		OutputStream out = null;
		try {
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition", "attachment; filename=" + originalFilename);
			out = response.getOutputStream();
			out.write(FileUtils.readFileToByteArray(file));
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 下载接口 
	 * http://localhost/download3?fileName=呵呵哒.rar
	 * @param id 文件id
	 * @param fileName 文件原始名称
	 * @throws IOException
	 */
	@RequestMapping("download3")
	public ResponseEntity<byte[]> download(Long id, String fileName) throws IOException {
		// 这些数据应该是通过某种方式从数据库取出来的比如id 。我图省事直接写了。
		String path = "D:\\upload_files\\file\\20190503\\"; // 获取资源存储路径
		String fileName = "1_20190503031553.rar"; // 存在服务器硬盘上的文件名
		
		File file = new File(path , fileName );
		String originalFilename= new String(fileName.getBytes("gb2312"), "iso8859-1");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", originalFilename);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值