1、代码实现 FileUtils
package com.xdx97.frame.common.utils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class FileUtils {
private static String filePath;
private static String fileFolder = "uploadFolder";
static {
try {
filePath = new File("").getCanonicalPath() + "/"+fileFolder+"/";
}catch (Exception e){
throw new RuntimeException("获取文件基本路径失败");
}
}
public static String upload( MultipartFile file){
try {
String fileName = file.getOriginalFilename();
String suffix = fileName.split("\\.")[1];
String newFileName = DateUtils.getYear() + "/" +
DateUtils.getMonth() + "/" +
DateUtils.getDay() + "/" +
UUID.randomUUID().toString() + "." + suffix;
String newPath = filePath + newFileName;
File oldFile = new File(newPath);
if (!oldFile.getParentFile().exists()){
oldFile.getParentFile().mkdirs();
}
File file2 = new File(newPath);
file.transferTo(file2);
return newFileName;
} catch (IOException e) {
throw new RuntimeException("文件上传失败");
}
}
public static List<String> uploads( List<MultipartFile> files){
List<String> result = new ArrayList<>();
for (MultipartFile file : files){
result.add(upload(file));
}
return result;
}
public static void download(String fileName, HttpServletResponse response) {
OutputStream toClient = null;
try {
File file = new File(filePath + fileName);
String filename = file.getName();
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
InputStream fis = new BufferedInputStream(new FileInputStream(filePath + fileName));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
if (fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("gif")){
response.setHeader("Content-Type", "image/jpeg");
}else {
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.setContentType("application/octet-stream; charset=utf-8");
}
response.addHeader("Content-Length", "" + file.length());
response.setHeader("Access-Control-Allow-Origin", "*");
toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
} catch (IOException ex) {
ex.printStackTrace();
}finally {
try {
toClient.flush();
toClient.close();
}catch (Exception e){
e.printStackTrace();
System.out.println("文件上传关闭流失败!");
}
}
}
public static void downloadByNetwork(String filePath,HttpServletResponse response){
InputStream inputStream = null;
try {
URL url = new URL(filePath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int httpRequestCode = con.getResponseCode();
if(httpRequestCode != 200){
throw new Exception("文件下载错误,错误码:"+httpRequestCode);
}
inputStream = con.getInputStream();
String finalFileName = UUID.randomUUID().toString();
response.reset();
response.setContentType(URLConnection.getFileNameMap().getContentTypeFor(filePath));
response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
byte[] b = new byte[1024];
int len;
while ((len = inputStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
}catch (Exception e){
throw new RuntimeException("从网络中下载文件失败");
}finally {
try {
inputStream.close();
response.getOutputStream().close();
}catch (Exception e){
throw new RuntimeException("文件流关闭失败");
}
}
}
}
2、视频讲解
https://www.bilibili.com/video/BV1kf4y1i761?p=7