直接上代码吧
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);
}
}