|
@@ -1,11 +1,11 @@
|
|
|
package org.jeecg.modules.report.controller;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLDecoder;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -49,7 +49,7 @@ import org.jeecg.common.aspect.annotation.AutoLog;
|
|
|
public class InterlockDataReportController extends JeecgController<InterlockDataReport, IInterlockDataReportService> {
|
|
|
@Autowired
|
|
|
private IInterlockDataReportService interlockDataReportService;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 分页列表查询
|
|
|
*
|
|
@@ -71,7 +71,7 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
IPage<InterlockDataReport> pageList = interlockDataReportService.page(page, queryWrapper);
|
|
|
return Result.OK(pageList);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 添加
|
|
|
*
|
|
@@ -86,7 +86,7 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
interlockDataReportService.save(interlockDataReport);
|
|
|
return Result.OK("添加成功!");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 编辑
|
|
|
*
|
|
@@ -101,7 +101,7 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
interlockDataReportService.updateById(interlockDataReport);
|
|
|
return Result.OK("编辑成功!");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 通过id删除
|
|
|
*
|
|
@@ -116,7 +116,7 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
interlockDataReportService.removeById(id);
|
|
|
return Result.OK("删除成功!");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 批量删除
|
|
|
*
|
|
@@ -131,7 +131,7 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
this.interlockDataReportService.removeByIds(Arrays.asList(ids.split(",")));
|
|
|
return Result.OK("批量删除成功!");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 通过id查询
|
|
|
*
|
|
@@ -174,4 +174,124 @@ public class InterlockDataReportController extends JeecgController<InterlockData
|
|
|
return super.importExcel(request, response, InterlockDataReport.class);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @ApiOperation("文件下载")
|
|
|
+ @GetMapping("/download")
|
|
|
+ public void fileDownload(HttpServletRequest request, HttpServletResponse response, InterlockDataReport interlockDataReport)
|
|
|
+ {
|
|
|
+ String path = interlockDataReport.getReportUrl() ;
|
|
|
+ try {
|
|
|
+ download(request,response,path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void download( HttpServletRequest request, HttpServletResponse response, String path)
|
|
|
+ throws Exception
|
|
|
+ {
|
|
|
+ String downloadPath = path;
|
|
|
+ File file = new File(downloadPath);
|
|
|
+ String downloadName = file.getName();
|
|
|
+
|
|
|
+ //Response.setContentType(MIME)的作用是使客户端的浏览器区分不同种类的数据,
|
|
|
+ // 并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
|
|
|
+ //一般在Servlet中,我们会首先设置请求以及响应的 内容类型和 编码方式:
|
|
|
+ response.setCharacterEncoding("utf-8");//编码方式
|
|
|
+// response.setContentType("multipart/form-data");//内容类型
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
+ "attachment;fileName=" + setFileDownloadHeader(request, downloadName));
|
|
|
+ //MIME信息头为Content-Disposition,Content-Type的类型为要下载的类型,这个信息头会告诉浏览器这个文件的名字和类型。
|
|
|
+
|
|
|
+ writeBytes(downloadPath, response.getOutputStream());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出指定文件的byte数组
|
|
|
+ *
|
|
|
+ * @param filePath 文件路径
|
|
|
+ * @param os 输出流
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static void writeBytes(String filePath, OutputStream os) throws IOException
|
|
|
+ {
|
|
|
+ FileInputStream fis = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists())
|
|
|
+ {
|
|
|
+ throw new FileNotFoundException(filePath);
|
|
|
+ }
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int length;
|
|
|
+ while ((length = fis.read(b)) > 0)
|
|
|
+ {
|
|
|
+ os.write(b, 0, length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e)
|
|
|
+ {
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ if (os != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ os.close();
|
|
|
+ }
|
|
|
+ catch (IOException e1)
|
|
|
+ {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ fis.close();
|
|
|
+ }
|
|
|
+ catch (IOException e1)
|
|
|
+ {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 下载文件名重新编码
|
|
|
+ *
|
|
|
+ * @param request 请求对象
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @return 编码后的文件名
|
|
|
+ */
|
|
|
+ public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
|
|
|
+ {
|
|
|
+ final String agent = request.getHeader("USER-AGENT");
|
|
|
+ String filename = fileName;
|
|
|
+ if (agent.contains("MSIE")||agent.contains("Trident"))
|
|
|
+ {// IE浏览器
|
|
|
+ filename = URLEncoder.encode(filename, "utf-8");
|
|
|
+ filename = filename.replace("+", " ");
|
|
|
+ }
|
|
|
+ else if (agent.contains("Firefox"))
|
|
|
+ {// 火狐浏览器
|
|
|
+ filename = new String(fileName.getBytes(), "ISO8859-1");
|
|
|
+ }
|
|
|
+ else if (agent.contains("Chrome"))
|
|
|
+ {// google浏览器
|
|
|
+ filename = URLEncoder.encode(filename, "utf-8");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {// 其它浏览器
|
|
|
+ filename = URLEncoder.encode(filename, "utf-8");
|
|
|
+ }
|
|
|
+ return filename;
|
|
|
+ }
|
|
|
+
|
|
|
}
|