package com.zy.crm.common.utils;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import lombok.Data;
|
import org.springframework.web.multipart.MultipartFile;
|
|
public class FileSaveExampleUtil {
|
|
// public static void main(String[] args) {
|
// String filePath = "D:/work/file/file.txt"; // 指定保存文件的路径
|
//
|
// try {
|
// String content = "这是要保存的文件内容";
|
// saveToFile(filePath, content);
|
// System.out.println("文件保存成功!");
|
// } catch (IOException e) {
|
// System.out.println("文件保存失败:" + e.getMessage());
|
// }
|
// }
|
|
public static void saveToFile(String filePath, String content) throws IOException {
|
File file = new File(filePath);
|
|
// 创建父目录(如果不存在)
|
File parentDir = file.getParentFile();
|
if (!parentDir.exists()) {
|
parentDir.mkdirs();
|
}
|
|
// 将内容写入文件
|
try (OutputStream outputStream = new FileOutputStream(file)) {
|
byte[] bytes = content.getBytes();
|
outputStream.write(bytes);
|
}
|
}
|
|
// public static void main(String[] args) {
|
// String savePath = "C:/path/to/save/"; // 指定保存文件的路径
|
// MultipartFile file = ...; // 获取要保存的MultipartFile文件
|
//
|
// try {
|
// saveFile(file, savePath);
|
// System.out.println("文件保存成功!");
|
// } catch (IOException e) {
|
// System.out.println("文件保存失败:" + e.getMessage());
|
// }
|
// }
|
|
public static void saveFile(MultipartFile file, String savePath) throws IOException {
|
// 创建保存文件的目录(如果不存在)
|
File directory = new File(savePath);
|
if (!directory.exists()) {
|
directory.mkdirs();
|
}
|
|
// 保存文件
|
String fileName = file.getOriginalFilename();
|
String filePath = savePath + fileName;
|
File dest = new File(filePath);
|
file.transferTo(dest);
|
}
|
|
public static void deleteFilesInDirectory(String directoryPath) {
|
File directory = new File(directoryPath);
|
if (directory.exists() && directory.isDirectory()) {
|
File[] files = directory.listFiles();
|
if (files != null) {
|
for (File file : files) {
|
if (file.isFile()) {
|
file.delete();
|
System.out.println("Deleted file: " + file.getAbsolutePath());
|
}
|
}
|
}
|
}
|
}
|
|
public static List<FileDTO> viewFileList(String directoryPath) {
|
List<FileDTO> fileList = new ArrayList<>();
|
|
File directory = new File(directoryPath);
|
if (directory.exists() && directory.isDirectory()) {
|
File[] files = directory.listFiles();
|
if (files != null) {
|
for (File file : files) {
|
if (file.isFile()) {
|
FileDTO fileDTO = new FileDTO(file.getName(), file.length(), file.getAbsolutePath());
|
// FileDTO fileDTO = new FileDTO(file.getName(), file.length());
|
fileList.add(fileDTO);
|
}
|
}
|
}
|
}
|
|
return fileList;
|
}
|
|
@Data
|
public static class FileDTO {
|
private String name;
|
private long size;
|
private String path;
|
|
public FileDTO(String name, long size, String path) {
|
this.name = name;
|
this.size = size;
|
this.path = path;
|
}
|
|
public FileDTO(String name, long size) {
|
this.name = name;
|
this.size = size;
|
// this.path = path;
|
}
|
|
// getters and setters
|
}
|
|
// public static void main(String[] args) {
|
// String directoryPath = "/path/to/directory";
|
// deleteFilesInDirectory(directoryPath);
|
// }
|
}
|