springBoot文件下载 - 从单个文件到多个文件一次性下载
一、单个文件下载
1、代码
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.tyky.common.utils.file.FileUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/dev-api/file")
public class DownloadController {
@GetMapping("/download")
public void download (HttpServletRequest request, HttpServletResponse response) throws IOException {
// 要下载的文件
Path file = Paths.get("C:\\Users\\ASUS\\Pictures\\壁纸\\素质过硬.jpg");
// 获取文件的媒体类型
String contentType = Files.probeContentType(file);
if (contentType == null) {
// 如果获取失败,则使用通用的文件类型
contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
}
response.setContentType(contentType);
// 文件大小
response.setContentLengthLong(Files.size(file));
//解决文件乱码
FileUtils.setAttachmentResponseHeader(response, String.valueOf(file.getFileName()));
// 响应数据给客户端
Files.copy(file, response.getOutputStream());
}
}
2、结果
二、多个文件下载 - 打包成 ZIP
1、代码
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.tyky.common.utils.file.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
@RequestMapping("/dev-api/file")
public class DownloadController {
@GetMapping("/downloadToZip")
public void zipDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 要下载的文件列表
List<Path> files = Arrays.asList(Paths.get("C:\\Users\\ASUS\\Pictures\\壁纸\\素质过硬.jpg"),
Paths.get("C:\\Users\\ASUS\\Pictures\\壁纸\\张伟请坐3.jpg"),
Paths.get("C:\\Users\\ASUS\\Pictures\\壁纸\\张伟专业团队.jpg"));
// zip压缩
response.setContentType("application/zip");
// 解决文件乱码
FileUtils.setAttachmentResponseHeader(response,"爱情公寓.zip");
// 压缩多个文件到zip文件中,并且响应给客户端
try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
for (Path file : files) {
try (InputStream inputStream = Files.newInputStream(file)) {
zipOutputStream.putNextEntry(new ZipEntry(file.getFileName().toString()));
StreamUtils.copy(inputStream, zipOutputStream);
zipOutputStream.flush();
}
}
}
}
}
2、结果
三、解决文件名称乱码工具类
public class FileUtils extends org.apache.commons.io.FileUtils
{
/**
* 下载文件名重新编码
*
* @param response 响应对象
* @param realFileName 真实文件名
* @return
*/
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
{
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(percentEncodedFileName)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(percentEncodedFileName);
response.setHeader("Content-disposition", contentDispositionValue.toString());
}
/**
* 百分号编码工具方法
*
* @param s 需要百分号编码的字符串
* @return 百分号编码后的字符串
*/
public static String percentEncode(String s) throws UnsupportedEncodingException
{
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
return encode.replaceAll("\\+", "%20");
}
}