|
@@ -0,0 +1,104 @@
|
|
|
+package com.dayou.utils;
|
|
|
+
|
|
|
+import com.dayou.exception.BusinessException;
|
|
|
+import org.springframework.util.FileCopyUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+public class FileNetUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件到服务器本地磁盘
|
|
|
+ *
|
|
|
+ * @param multiFile 文件
|
|
|
+ * @param baseDir 根路径
|
|
|
+ * @param uploadPath 服务器上要存储文件的路径
|
|
|
+ * @param uploadFileName 服务器上要存储的文件的名称
|
|
|
+ * @return Map<String, String> 文件信息
|
|
|
+ */
|
|
|
+ public static Map<String, String> uploadToServer(MultipartFile multiFile, String baseDir, String uploadPath, String uploadFileName) {
|
|
|
+ //构建文件对象
|
|
|
+ File file = new File(baseDir + uploadPath);
|
|
|
+ //文件目录不存在则递归创建目录
|
|
|
+ if (!file.exists()) {
|
|
|
+ boolean mkdirs = file.mkdirs();
|
|
|
+ if (!mkdirs) {
|
|
|
+ throw new BusinessException("创建文件夹异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ //获取文件输入流
|
|
|
+ InputStream inputStream = multiFile.getInputStream();
|
|
|
+ //构建文件输出流
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(baseDir + uploadPath + uploadFileName);
|
|
|
+ // SpringBoot自带工具类上传
|
|
|
+ FileCopyUtils.copy(inputStream, outputStream);
|
|
|
+ Map<String, String> fileInfo = new HashMap<>();
|
|
|
+ fileInfo.put("fileName", uploadFileName);
|
|
|
+ fileInfo.put("filePath", uploadPath);
|
|
|
+ return fileInfo;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BusinessException("文件上传异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过url从远端下载文件到服务器本地磁盘
|
|
|
+ *
|
|
|
+ * @param downloadUrl 要下载的文件的地址
|
|
|
+ * @param downloadPath 服务器上存储的文件路径
|
|
|
+ * @param downloadFileName 服务器上存储的文件名称
|
|
|
+ * @return Map<String, String> 文件信息
|
|
|
+ */
|
|
|
+ public static Map<String, String> downloadToServer(String downloadUrl, String downloadPath, String downloadFileName) {
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ URL url = new URL(downloadUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.connect();
|
|
|
+ bis = new BufferedInputStream(connection.getInputStream());
|
|
|
+ File file = new File(downloadPath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ boolean mkdirs = file.mkdirs();
|
|
|
+ if (!mkdirs) {
|
|
|
+ throw new BusinessException("创建文件目录失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String filePathName = downloadPath + File.separator + downloadFileName;
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ int size;
|
|
|
+ fos = new FileOutputStream(filePathName);
|
|
|
+ while ((size = bis.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, size);
|
|
|
+ }
|
|
|
+ Map<String, String> fileInfo = new HashMap<>();
|
|
|
+ fileInfo.put(downloadFileName, downloadPath);
|
|
|
+ return fileInfo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BusinessException("下载文件异常", e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (bis != null) {
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+ if (fos != null) {
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BusinessException("关流异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|