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