|
@@ -0,0 +1,259 @@
|
|
|
+package com.lsw.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jfinal.kit.PathKit;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.Date;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+import static java.time.LocalDateTime.now;
|
|
|
+
|
|
|
+public class ExportController extends BaseController{
|
|
|
+ private static final int BUFFER_SIZE = 2 * 1024;
|
|
|
+
|
|
|
+ public void index() {
|
|
|
+
|
|
|
+ String json="{\n" +
|
|
|
+ " \"results\": [\n" +
|
|
|
+ " {\n" +
|
|
|
+ "\t \"accessToken\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTY0MzM4OTA4MCwidXNlcm5hbWUiOiJfX2pGRlpZdkxJRzZ1RmM3Q2RCTWcifQ.bMVXPUZu6r7aIgdRiItf4j42v6JYtU_Xlhs25f5pYRk\",\n" +
|
|
|
+ " \"index\": [\n" +
|
|
|
+ "\t {\"name\":\"159357.jpg\",\"path\":\"http://bpic.588ku.com/element_origin_min_pic/19/03/15/75076c485081d15ed9c224ad3e4ce4a1.jpg\",\"size\":\"39424\",\"userName\":\"SOM\",\"time\":\"2022-12-08 15:41:43\"},\n" +
|
|
|
+ "\t {\"name\":\"Deel.xls\",\"path\":\"http://192.168.0.251/obpm/uploads/item/2022/1/a218eab9-2b57-437e-9053-f97c95cec9ac.xls\",\"size\":\"39424\",\"userName\":\"SOM\",\"time\":\"2022-12-08 15:41:43\"},\n" +
|
|
|
+ "\t {\"name\":\"席澳冬简历(1).pdf\",\"path\":\"http://192.168.0.251/obpm/uploads/item/2022/1/d6d6518a-69d4-417f-acdf-15e262801860.pdf\",\"size\":\"150965\",\"userName\":\"SOM\",\"time\":\"2022-12-08 15:41:43\"},\n" +
|
|
|
+ "\t {\"name\":\"国医大师黄瑾明教授学术信息数据管理平台 评审论证稿(修订模式).doc\",\"path\":\"http://192.168.0.251/obpm/uploads/item/2022/1/a1b2b180-cd21-49a1-a64f-a331bf0d0842.doc\",\"size\":\"1028103\",\"userName\":\"SOM\",\"time\":\"2022-11-28 16:18:02\"}\n" +
|
|
|
+ "\t ]\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}\n";
|
|
|
+ // String json= getRawData();
|
|
|
+ //这个属于发起请求并获取json数据(得引入hutool)
|
|
|
+ //String str = HttpUtil.createGet(url).execute().body();
|
|
|
+ //转化请求的 json 数据
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ Date dt = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
|
|
|
+ //获取 results 数组
|
|
|
+ JSONArray results = jsonObject.getJSONArray("results");
|
|
|
+ for (int i = 0; i < results.size(); i++) {
|
|
|
+ String accessToken = results.getJSONObject(i).getString("accessToken");
|
|
|
+ //获取index数组
|
|
|
+ JSONArray index = results.getJSONObject(i).getJSONArray("index");
|
|
|
+ //遍历 index 数组
|
|
|
+ for (int j = 0; j < index.size(); j++) {
|
|
|
+ //获取 title,下面的参数获取以此类推
|
|
|
+ String name = index.getJSONObject(j).getString("name");
|
|
|
+ //String path = "http://192.168.0.251/obpm"+ index.getJSONObject(j).getString("path");
|
|
|
+ String path = index.getJSONObject(j).getString("path");
|
|
|
+ //将文件保存到相对路径文件夹,文件夹名称以yyyyMMddhhmmss命名
|
|
|
+ downLoadFromUrl(path, name, PathKit.getWebRootPath()+"/download/"+sdf.format(dt)+"/",accessToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 待压缩的文件路径
|
|
|
+ //String fileSourcePath = "/download/";
|
|
|
+ String fileSourcePath = PathKit.getWebRootPath()+ "/download/"+sdf.format(dt)+"/";
|
|
|
+
|
|
|
+ System.out.println("now--"+now());
|
|
|
+ // 压缩后存放路径
|
|
|
+
|
|
|
+ //String zipTargetPath = "/upload/"+sdf.format(dt)+"zip";
|
|
|
+ String zipTargetPath = PathKit.getWebRootPath()+ "/upload/"+sdf.format(dt)+".zip";
|
|
|
+ // 压缩后文件的名称
|
|
|
+ //String fileName = "20221208";
|
|
|
+ //boolean flag = fileToZip(fileSourcePath, zipTargetPath, fileName);
|
|
|
+
|
|
|
+ boolean flag2 = toZip(fileSourcePath, zipTargetPath,true);
|
|
|
+ System.out.printf(flag2 ? "文件打包成功!" : "文件打包失败!");
|
|
|
+ // downLoadFromUrl("http://bpic.588ku.com/element_origin_min_pic/19/03/15/75076c485081d15ed9c224ad3e4ce4a1.jpg", "159357.jpg", "D:\\file\\");
|
|
|
+
|
|
|
+ renderFile("http://192.168.0.102:9999/upload/"+sdf.format(dt)+".zip");
|
|
|
+
|
|
|
+ System.out.println(" 下载======"+ "http://192.168.0.102:9999/upload/"+sdf.format(dt)+".zip");
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 网络Url下载文件
|
|
|
+ * @Author yy
|
|
|
+ * @param urlStr 文件地址
|
|
|
+ * @param fileName 新FileName
|
|
|
+ * @param savePath 输出地址
|
|
|
+ */
|
|
|
+ public static String downLoadFromUrl(String urlStr, String fileName, String savePath,String accessToken) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestProperty("accessToken",accessToken);
|
|
|
+ // 设置超时间为3秒
|
|
|
+ conn.setConnectTimeout(5 * 1000);
|
|
|
+ // 防止屏蔽程序抓取而返回403错误
|
|
|
+ conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
+
|
|
|
+ // 得到输入流
|
|
|
+ InputStream inputStream = conn.getInputStream();
|
|
|
+ // 获取字节数组
|
|
|
+ byte[] getData = readInputStream(inputStream);
|
|
|
+ // System.out.println(fileName + "getData.length()===" + getData.length);
|
|
|
+ // 文件保存位置
|
|
|
+ File saveDir = new File(savePath);
|
|
|
+ if (!saveDir.exists()) {
|
|
|
+ saveDir.mkdir();
|
|
|
+ }
|
|
|
+ File file = new File(saveDir + File.separator + fileName);
|
|
|
+ FileOutputStream fos = new FileOutputStream(file);
|
|
|
+ fos.write(getData);
|
|
|
+ if (fos != null) {
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ return saveDir + File.separator + fileName;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从输入流中获取字节数组
|
|
|
+ *
|
|
|
+ * @param inputStream
|
|
|
+ * @throws IOException
|
|
|
+ * @Author: yy
|
|
|
+ */
|
|
|
+ public static byte[] readInputStream(InputStream inputStream) throws IOException {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ while ((len = inputStream.read(buffer)) != -1) {
|
|
|
+ bos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ bos.close();
|
|
|
+ return bos.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩成ZIP
|
|
|
+ * @param srcFilePath 压缩文件路径
|
|
|
+ * @param tarFilePath 目标ZIP输出路径
|
|
|
+ * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
|
|
+ * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
+ * @throws Exception 压缩失败会抛出异常
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ public static boolean toZip(String srcFilePath, String tarFilePath, boolean KeepDirStructure) {
|
|
|
+
|
|
|
+ boolean isCompressSuccess = false;
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ org.apache.tools.zip.ZipOutputStream zos = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ File sourceFile = new File(srcFilePath);
|
|
|
+
|
|
|
+ if (!sourceFile.exists()) {
|
|
|
+ throw new FileNotFoundException("待压缩文件 [" + srcFilePath + "]不存在.");
|
|
|
+ }
|
|
|
+
|
|
|
+ fos = new FileOutputStream(new File(tarFilePath));
|
|
|
+ zos = new org.apache.tools.zip.ZipOutputStream(fos);
|
|
|
+
|
|
|
+ // 设置压缩的编码,解决压缩路径中的中文乱码问题
|
|
|
+ zos.setEncoding("GBK");
|
|
|
+ compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure);
|
|
|
+ isCompressSuccess = true;
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ System.out.println("【文件压缩】 压缩完成,耗时:{} ms"+ (end - start));
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("【文件压缩】 压缩失败"+ e.toString());
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ closeOutPutStream(zos);
|
|
|
+ closeOutPutStream(fos);
|
|
|
+ }
|
|
|
+ return isCompressSuccess;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归压缩方法
|
|
|
+ * @param sourceFile 源文件
|
|
|
+ * @param zos zip输出流
|
|
|
+ * @param name 压缩后的名称
|
|
|
+ * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
|
|
+ * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+
|
|
|
+ private static void compress(File sourceFile, org.apache.tools.zip.ZipOutputStream zos, String name, boolean KeepDirStructure)
|
|
|
+ throws Exception {
|
|
|
+
|
|
|
+ byte[] buf = new byte[BUFFER_SIZE];
|
|
|
+ if (sourceFile.isFile()) {
|
|
|
+ // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
|
|
+ zos.putNextEntry(new org.apache.tools.zip.ZipEntry(name));
|
|
|
+ // copy文件到zip输出流中
|
|
|
+ int len;
|
|
|
+
|
|
|
+ FileInputStream in = new FileInputStream(sourceFile);
|
|
|
+ while ((len = in.read(buf)) != -1) {
|
|
|
+ zos.write(buf, 0, len);
|
|
|
+ }
|
|
|
+
|
|
|
+ zos.closeEntry();
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ } else {
|
|
|
+ File[] listFiles = sourceFile.listFiles();
|
|
|
+ if (listFiles == null || listFiles.length == 0) {
|
|
|
+ // 需要保留原来的文件结构时,需要对空文件夹进行处理
|
|
|
+ if (KeepDirStructure) {
|
|
|
+ // 空文件夹的处理
|
|
|
+ zos.putNextEntry(new org.apache.tools.zip.ZipEntry(name + "/"));
|
|
|
+ // 没有文件,不需要文件的copy
|
|
|
+ zos.closeEntry();
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ for (File file : listFiles) {
|
|
|
+ // 判断是否需要保留原来的文件结构
|
|
|
+ if (KeepDirStructure) {
|
|
|
+ // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
|
|
+ // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
|
|
+ compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
|
|
|
+ } else {
|
|
|
+ compress(file, zos, file.getName(), KeepDirStructure);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 释放资源
|
|
|
+ * @Title closeOutPutStream
|
|
|
+ * @param ops
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public static void closeOutPutStream(OutputStream ops) {
|
|
|
+ if (ops != null) {
|
|
|
+ try {
|
|
|
+ ops.close();
|
|
|
+ } catch(IOException ex) {
|
|
|
+ System.out.println("关闭输出流失败"+ ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|