import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class DownloadUtil {
/**
* 下載檔案(適用大多數檔案型別,如: word,excel,pdf,mp4 等)
*
* @param fileName 定義檔名(需帶副檔名,如: .docx)
* @param fileBytes
* @param response
*/
public static void downloadFile(String fileName, byte[] fileBytes, HttpServletResponse response) {
if (StringUtils.isBlank(fileName)) {
throw new RuntimeException("檔名不能為空");
}
String[] split = fileName.split(".");
if (split.length == 1) {
throw new RuntimeException("檔名需帶副檔名");
}
try {
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
// application/octet-stream: 二進位制流傳輸(適用各種檔案型別)
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] file2bytes(File file) {
return FileUtil.readBytes(file);
}
public static byte[] inputStream2bytes(InputStream inputStream, boolean isClose) {
return IoUtil.readBytes(inputStream, isClose);
}
/**
* 下載 Excel 檔案
*
* @param fileName
* @param workbook
* @param response
*/
@SneakyThrows
public static void downloadExcelByWorkbook(String fileName, Workbook workbook, HttpServletResponse response) {
OutputStream os = null;
try {
if (!fileName.contains(".")) {
fileName = fileName + ".xlsx";
}
response.setContentType("application/*");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
response.setCharacterEncoding("UTF-8");
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (Exception ignored) {
} finally {
os.close();
}
}
}
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@RestController
public class UploadFileController {
@SneakyThrows
@PostMapping(value = "/upload")
public void uploadMultipartFile(MultipartFile file) {
File destFile = new File("E:" + File.separator + "upload-test-" + System.currentTimeMillis() + ".xlsx");
file.transferTo(destFile);
}
@SneakyThrows
@PostMapping(value = "/upload-binary-request")
public void uploadBinary(HttpServletRequest request) {
System.err.println(request.getContentType());
File destFile = new File("E:" + File.separator + "upload-binary-test_" + System.currentTimeMillis() + ".xlsx");
FileUtils.copyInputStreamToFile(request.getInputStream(), destFile);
}
@SneakyThrows
@PostMapping(value = "/upload-binary-bytes")
public void uploadBytes(@RequestBody byte[] bytes) {
File destFile = new File("E:" + File.separator + "upload-bytes-test_" + System.currentTimeMillis() + ".xlsx");
FileUtils.copyInputStreamToFile(IoUtil.toStream(bytes), destFile);
}
@SneakyThrows
@PostMapping(value = "/upload-base64")
public void uploadBase64(String base64) {
File destFile = new File("E:" + File.separator + "upload-string-test_" + System.currentTimeMillis() + ".xlsx");
Base64.decodeToFile(base64, destFile);
}
@Test
public void testUploadMultipartFile() {
File file = new File("E:" + File.separator + "test.xlsx");
Map<String, Object> map = new HashMap<>();
map.put("file", file);
HttpUtil.post("http://localhost:8080/upload", map);
}
@Test
public void testUploadBase64() {
File file = new File("E:" + File.separator + "test.xlsx");
Map<String, Object> map = new HashMap<>();
map.put("base64", Base64.encode(file));
HttpUtil.post("http://localhost:8080/upload-base64", map);
}
}