yangyj 2 anni fa
parent
commit
6bc01ec5cc

+ 7 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/controller/FormulaBaseController.java

@@ -16,6 +16,8 @@ import org.springblade.manager.vo.FormulaBaseVo;
 import org.springframework.web.bind.annotation.*;
 
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 
@@ -79,5 +81,10 @@ public class FormulaBaseController extends BladeController {
         return R.data(this.service.getTypeMap());
     }
 
+    @GetMapping("/get-file")
+    public void getFile(HttpServletResponse response, String url) throws IOException {
+       this.service.download(response,url);
+    }
+
 
 }

+ 43 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/service/impl/FormulaBaseService.java

@@ -1,15 +1,26 @@
 package org.springblade.manager.service.impl;
 
+import com.bstek.ureport.console.designer.ReportUtils;
 import org.springblade.core.mp.base.BaseServiceImpl;
 import org.springblade.core.tool.utils.CollectionUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.ResourceUtil;
 import org.springblade.manager.entity.FormulaBase;
 import org.springblade.manager.mapper.FormulaBaseMapper;
 import org.springblade.manager.vo.FormulaBaseVo;
 import org.springblade.manager.vo.FormulaType;
 import org.springframework.beans.BeanUtils;
+import org.springframework.core.io.Resource;
 import org.springframework.stereotype.Service;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
+import java.util.regex.Matcher;
 import java.util.stream.Collectors;
 
 /**
@@ -32,4 +43,36 @@ public class FormulaBaseService extends BaseServiceImpl<FormulaBaseMapper, Formu
         return null;
     }
 
+    public  void  download(HttpServletResponse response, String url) throws IOException {
+        Resource resource =ResourceUtil.getResource(url);
+        if(resource.isFile()){
+            int index = url.lastIndexOf("/");
+            String urls = url.substring(0, index + 1);
+            String name = url.substring(index + 1);
+            url = urls + URLEncoder.encode(name, "UTF-8");
+            BufferedInputStream bis = new BufferedInputStream(resource.getInputStream());
+            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
+            String[] fileNameList = url.split("/");
+            // 设置文件ContentType类型,这样设置,会自动判断下载文件类型
+            response.setContentType("application/multipart/form-data");
+            String fileName = new String(fileNameList[fileNameList.length - 1].getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
+            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
+            try {
+                byte[] cache= new byte[4098];
+                int size;
+                while((size=bis.read(cache))>-1){
+                    bos.write(cache,0,size);
+                }
+                bos.flush();
+            } catch (IOException ioe) {
+                ioe.printStackTrace();
+            }finally {
+                bos.close();
+                bis.close();
+            }
+        }
+
+
+    }
+
 }