Origin修改

duhanyu
杜函宇 5 months ago
parent e49b39e327
commit 32c1de479c

@ -76,17 +76,37 @@ public class FileUtils {
* @throws IOException IO * @throws IOException IO
*/ */
public static String writeBytes(byte[] data, String uploadDir) throws IOException { public static String writeBytes(byte[] data, String uploadDir) throws IOException {
FileOutputStream fos = null; // FileOutputStream fos = null;
String pathName = ""; // String pathName = "";
try { // try {
// String extension = getFileExtendName(data);
// pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
// File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
// fos = new FileOutputStream(file);
// fos.write(data);
// } finally {
// IOUtils.close(fos);
// }
// return FileUploadUtils.getPathFileName(uploadDir, pathName);
if (data == null || data.length == 0) {
throw new IllegalArgumentException("数据不能为空");
}
String extension = getFileExtendName(data); String extension = getFileExtendName(data);
pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension; String pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName); File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
fos = new FileOutputStream(file);
// 确保父目录存在
file.getParentFile().mkdirs();
// 使用 try-with-resources 自动管理 FileOutputStream 的生命周期
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(data); fos.write(data);
} finally { } catch (IOException e) {
IOUtils.close(fos); // 可以选择记录日志或抛出自定义异常
throw new IOException("文件写入失败: " + e.getMessage(), e);
} }
return FileUploadUtils.getPathFileName(uploadDir, pathName); return FileUploadUtils.getPathFileName(uploadDir, pathName);
} }

Loading…
Cancel
Save