Parcourir la source

光伏逆变器负荷率利用小时接口更新

xushili il y a 2 ans
Parent
commit
de9484b4f4

+ 113 - 0
src/main/java/com/gyee/frame/common/file/FileZip.java

@@ -0,0 +1,113 @@
+package com.gyee.frame.common.file;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URLEncoder;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+public class FileZip {
+    /**
+     * 加载文件
+     *
+     * @param fileName
+     * @return
+     */
+    public static void download(String fileName, HttpServletResponse response) {
+        FileInputStream fis = null;
+        BufferedInputStream bis = null;
+        try {
+            // 获取文件
+            File file = new File(fileName);
+            // 清空缓冲区,状态码和响应头(headers)
+            response.reset();
+            // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
+            response.setContentType("application/octet-stream;charset=utf-8");
+            // 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
+            response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName() + ";filename*=utf-8''" + URLEncoder.encode(file.getName(), "utf-8"));
+
+            // 实现文件下载
+            byte[] buffer = new byte[1024];
+            fis = new FileInputStream(file);
+            bis = new BufferedInputStream(fis);
+            // 获取字节流
+            OutputStream os = response.getOutputStream();
+            int i = bis.read(buffer);
+            while (i != -1) {
+                os.write(buffer, 0, i);
+                i = bis.read(buffer);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (bis != null) {
+                try {
+                    bis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    /**
+     * @describe 压缩多个文件
+     * @author zfc
+     */
+    public static String zipFiles(List<File> srcFiles, File zipFile) {
+        // 判断压缩后的文件存在不,不存在则创建
+        if (!zipFile.exists()) {
+            try {
+                zipFile.createNewFile();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+            // 创建 FileOutputStream 对象
+            FileOutputStream fileOutputStream = null;
+            // 创建 ZipOutputStream
+            ZipOutputStream zipOutputStream = null;
+            // 创建 FileInputStream 对象
+            FileInputStream fileInputStream = null;
+
+            try {
+                // 实例化 FileOutputStream 对象
+                fileOutputStream = new FileOutputStream(zipFile);
+                // 实例化 ZipOutputStream 对象
+                zipOutputStream = new ZipOutputStream(fileOutputStream);
+                // 创建 ZipEntry 对象
+                ZipEntry zipEntry = null;
+                // 遍历源文件数组
+                for (int i = 0; i < srcFiles.size(); i++) {
+                    // 将源文件数组中的当前文件读入 FileInputStream 流中
+                    fileInputStream = new FileInputStream(srcFiles.get(i));
+                    // 实例化 ZipEntry 对象,源文件数组中的当前文件
+                    zipEntry = new ZipEntry(srcFiles.get(i).getName());
+                    zipOutputStream.putNextEntry(zipEntry);
+                    // 该变量记录每次真正读的字节个数
+                    int len;
+                    // 定义每次读取的字节数组
+                    byte[] buffer = new byte[1024];
+                    while ((len = fileInputStream.read(buffer)) > 0) {
+                        zipOutputStream.write(buffer, 0, len);
+                    }
+                }
+                zipOutputStream.closeEntry();
+                zipOutputStream.close();
+                fileInputStream.close();
+                fileOutputStream.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return zipFile.getAbsolutePath();
+    }
+}

+ 22 - 6
src/main/java/com/gyee/frame/controller/file/LoadrateHourlyController.java

@@ -2,6 +2,8 @@ package com.gyee.frame.controller.file;
 
 import com.gyee.frame.common.conf.AjaxStatus;
 import com.gyee.frame.common.domain.AjaxResult;
+import com.gyee.frame.common.file.FileZip;
+import com.gyee.frame.model.config.FilePathConfig;
 import com.gyee.frame.model.excel.LoadrateHourly;
 import com.gyee.frame.service.export.LoadrateHourlyService;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -10,8 +12,11 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import java.io.File;
+import java.util.Collection;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/loadrate")
@@ -34,13 +39,24 @@ public class LoadrateHourlyController {
         return AjaxResult.successData(AjaxStatus.success.code,loadrateHourlies);
     }
     @GetMapping("/down")
-    public AjaxResult getDownInfo(@RequestParam(value = "stationid",required = false) String stationid,
+    public AjaxResult getDownInfo(HttpServletResponse response,
+                                  @RequestParam(value = "stationid",required = false) String stationid,
                                   @RequestParam("date") String date,
-                                  @RequestParam("time") String time){
-        File file = loadrateHourlyService.getPath(date, time);
-        if(file.exists()){
-            return AjaxResult.successData(AjaxStatus.success.code,file.getPath());
+                                  @RequestParam(value = "time",required = false) String time){
+
+        if(time!=null){
+            File path = loadrateHourlyService.getPath(date, time);
+            if(!path.exists()) return AjaxResult.error("文件不存在!");
+            FileZip.download(path.getPath(),response);
+            return AjaxResult.success();
+        }else{
+            String s = FilePathConfig.getDataPath() + "\\光伏逆变器负荷率利用小时计算\\" + date.replaceAll("-", "") + ".zip";
+            File zipfile = new File(s);
+            List<File> values = loadrateHourlyService.getAllPath(date).entrySet().stream().map(kv -> kv.getValue()).collect(Collectors.toList());
+            if(values.size()<=0) return AjaxResult.error("文件不存在!");
+            String s1 = FileZip.zipFiles(values, zipfile);
+            FileZip.download(s1,response);
+            return AjaxResult.success();
         }
-        return AjaxResult.error("文件不存在!");
     }
 }

+ 3 - 3
src/main/java/com/gyee/frame/service/export/LoadrateHourlyService.java

@@ -46,9 +46,9 @@ public class LoadrateHourlyService {
         return tpMap;
     }
 
-    public File getPath(String time, String s) {
-        String stime = time.replaceAll("-", "");
-        String path = FilePathConfig.getDataPath() + "\\光伏逆变器负荷率利用小时计算\\" + stime + s + ".xlsx";
+    public File getPath(String date, String time) {
+        String stime = date.replaceAll("-", "");
+        String path = FilePathConfig.getDataPath() + "\\光伏逆变器负荷率利用小时计算\\" + stime + time + ".xlsx";
         return new File(path);
     }