package com.daidai.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.util.List;
/**
* Author: Mr.Chen
* Date: 2022/09/17 10:16
* Description: 文件上传与下载
*/
@RestController
public class FileController {
@RequestMapping("/upload")
public String fileUpload(@RequestParam("file") MultipartFile file) {
try {
if (file.isEmpty()) {
return "文件为空";
}
String filename = file.getOriginalFilename();
String filePath = "./file/";
String path = filePath + filename;
File dest = new File(new File(path).getAbsolutePath());
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
return "上传成功";
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return "上传失败";
}
@RequestMapping("/batch")
public String batchUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); i++) {
file = files.get(i);
String filePath = "./file/";
File dest = new File(filePath);
if (!dest.exists()) {
dest.mkdirs();
}
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(filePath + file.getOriginalFilename())
));
stream.write(bytes);
stream.close();
} catch (IOException e) {
e.printStackTrace();
stream = null;
return i + "<==上传失败!";
}
} else {
return i + "<==文件为空,上传失败";
}
}
return "上传成功";
}
private static String getBrowser(HttpServletRequest request) {
String UserAgent = request.getHeader("USER-AGENT").toLowerCase();
if (UserAgent != null) {
if (UserAgent.indexOf("msie") != -1) {
return "IE";
}
if (UserAgent.indexOf("firefox") != -1) {
return "FF";
}
if (UserAgent.indexOf("safari") != -1) {
return "SF";
}
}
return null;
}
@RequestMapping("/download")
@ResponseBody
public String fileDownload(HttpServletResponse res, HttpServletRequest request) {
String fileName = "xxx.txt";
String filePath = "D:/uploadFile";
File file = new File(filePath + "/" + fileName);
System.out.println(file);
if (file.exists()) {//判断文件是否存在
//判断浏览器是否为火狐
try {
if ("FF".equals(getBrowser(request))) {
// 火狐浏览器 设置编码new String(realName.getBytes("GB2312"), "ISO-8859-1");
fileName = new String(fileName.getBytes("GB2312"), "ISO-8859-1");
} else {
fileName = URLEncoder.encode(fileName, "UTF-8");//encode编码UTF-8 解决大多数中文乱码
fileName = fileName.replace("+", "%20");//encode后替换空格 解决空格问题
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
res.setContentType("application/force-download");//设置强制下载
res.setHeader("Content-Disposition", "attachment;filename=" + fileName);//设置文件名
byte[] buff = new byte[1024];// 用来存储每次读取到的字节数组
//创建输入流(读文件)输出流(写文件)
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
// bis = new BufferedInputStream(new FileInputStream(file));
bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
return "文件不存在!!!";
}
return "download success";
}
}
springboot 文件上传与下载
最新推荐文章于 2024-10-31 12:02:50 发布