|
@@ -3,36 +3,34 @@ package com.lsw.controller;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.jfinal.kit.PathKit;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
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();
|
|
|
+ System.out.println("zzzzzzzzzzzzzzz");
|
|
|
+// String json="{\n" +
|
|
|
+// " \"results\": [\n" +
|
|
|
+// " {\n" +
|
|
|
+// "\t \"accessToken\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTY0MzQ1OTkxMywidXNlcm5hbWUiOiJfX2pGRlpZdkxJRzZ1RmM3Q2RCTWcifQ.CTh-0NUgwVjqgeSqk179XThTGC5nKzXkQwEwbIDFyIU\",\n" +
|
|
|
+// " \"index\": [\n" +
|
|
|
+// "\t {\"name\":\"Deel.xls\",\"path\":\"/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\":\"/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\":\"/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 数据
|
|
@@ -49,8 +47,8 @@ public class ExportController extends BaseController{
|
|
|
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");
|
|
|
+ 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);
|
|
|
}
|
|
@@ -59,9 +57,9 @@ public class ExportController extends BaseController{
|
|
|
|
|
|
// 待压缩的文件路径
|
|
|
//String fileSourcePath = "/download/";
|
|
|
- String fileSourcePath = PathKit.getWebRootPath()+ "/download/"+sdf.format(dt)+"/";
|
|
|
+ String fileSourcePath = PathKit.getWebRootPath()+ "/download/"+sdf.format(dt)+"/";
|
|
|
|
|
|
- System.out.println("now--"+now());
|
|
|
+ // System.out.println("now--"+now());
|
|
|
// 压缩后存放路径
|
|
|
|
|
|
//String zipTargetPath = "/upload/"+sdf.format(dt)+"zip";
|
|
@@ -74,9 +72,15 @@ public class ExportController extends BaseController{
|
|
|
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");
|
|
|
-
|
|
|
+ //renderFile("http://192.168.0.102:9999/upload/"+sdf.format(dt)+".zip");
|
|
|
+// try {
|
|
|
+// outputFile(sdf.format(dt)+".zip");
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
System.out.println(" 下载======"+ "http://192.168.0.102:9999/upload/"+sdf.format(dt)+".zip");
|
|
|
+ //render("index.html");
|
|
|
+ renderJson("{'url':'http://192.168.0.102:9999/upload/"+sdf.format(dt)+".zip'}");
|
|
|
}
|
|
|
/**
|
|
|
* 网络Url下载文件
|
|
@@ -99,7 +103,7 @@ public class ExportController extends BaseController{
|
|
|
InputStream inputStream = conn.getInputStream();
|
|
|
// 获取字节数组
|
|
|
byte[] getData = readInputStream(inputStream);
|
|
|
- // System.out.println(fileName + "getData.length()===" + getData.length);
|
|
|
+ // System.out.println(fileName + "getData.length()===" + getData.length);
|
|
|
// 文件保存位置
|
|
|
File saveDir = new File(savePath);
|
|
|
if (!saveDir.exists()) {
|
|
@@ -139,10 +143,6 @@ public class ExportController extends BaseController{
|
|
|
return bos.toByteArray();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 压缩成ZIP
|
|
|
* @param srcFilePath 压缩文件路径
|
|
@@ -150,7 +150,6 @@ public class ExportController extends BaseController{
|
|
|
* @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
|
|
|
* false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
|
|
|
* @throws Exception 压缩失败会抛出异常
|
|
|
-
|
|
|
*/
|
|
|
|
|
|
public static boolean toZip(String srcFilePath, String tarFilePath, boolean KeepDirStructure) {
|
|
@@ -202,7 +201,7 @@ public class ExportController extends BaseController{
|
|
|
|
|
|
byte[] buf = new byte[BUFFER_SIZE];
|
|
|
if (sourceFile.isFile()) {
|
|
|
- // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
|
|
+ // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
|
|
|
zos.putNextEntry(new org.apache.tools.zip.ZipEntry(name));
|
|
|
// copy文件到zip输出流中
|
|
|
int len;
|
|
@@ -218,7 +217,7 @@ public class ExportController extends BaseController{
|
|
|
} else {
|
|
|
File[] listFiles = sourceFile.listFiles();
|
|
|
if (listFiles == null || listFiles.length == 0) {
|
|
|
- // 需要保留原来的文件结构时,需要对空文件夹进行处理
|
|
|
+ // 需要保留原来的文件结构时,需要对空文件夹进行处理
|
|
|
if (KeepDirStructure) {
|
|
|
// 空文件夹的处理
|
|
|
zos.putNextEntry(new org.apache.tools.zip.ZipEntry(name + "/"));
|
|
@@ -230,8 +229,8 @@ public class ExportController extends BaseController{
|
|
|
for (File file : listFiles) {
|
|
|
// 判断是否需要保留原来的文件结构
|
|
|
if (KeepDirStructure) {
|
|
|
- // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
|
|
- // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
|
|
+ // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
|
|
|
+ // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
|
|
|
compress(file, zos, name + "/" + file.getName(), KeepDirStructure);
|
|
|
} else {
|
|
|
compress(file, zos, file.getName(), KeepDirStructure);
|
|
@@ -256,4 +255,59 @@ public class ExportController extends BaseController{
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ @CrossOrigin
|
|
|
+ public void outputFile(String fileName) throws IOException {
|
|
|
+
|
|
|
+// File file=new File(filePath);
|
|
|
+//
|
|
|
+// if( file.exists()) //如果filePath下的文件是存在的
|
|
|
+//
|
|
|
+// {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //renderFile(file,"filename");
|
|
|
+ // renderFile("http://192.168.0.102:9999/upload/20221209060056.zip");
|
|
|
+ // renderFile(PathKit.getWebRootPath()+ "/upload/20221209060056.zip");
|
|
|
+
|
|
|
+// renderFile 中的参数可以是File类变量,输出文件的文件命名
|
|
|
+
|
|
|
+//文件命名(选填 一般是随机数:UUID.randomUUID().toString()+".文件类型")
|
|
|
+
|
|
|
+// }
|
|
|
+ String basePath = PathKit.getWebRootPath() +"/upload";
|
|
|
+
|
|
|
+ //获取前台的传递的文件名(包括文件格式,例如"test.jpg")
|
|
|
+
|
|
|
+
|
|
|
+ //根据根目录和文件名,拼接成完整的file路径
|
|
|
+ String targetPath = basePath+File.separator+fileName;
|
|
|
+ System.out.println("targetPath===="+targetPath);
|
|
|
+ //普通IO流实现下载的功能
|
|
|
+ HttpServletResponse res = getResponse(); //创建response回应
|
|
|
+ res.setContentType("text/html; charset=UTF-8"); //设置编码字符
|
|
|
+ res.setContentType("application/x-msdownload"); //设置内容类型
|
|
|
+ res.setHeader("Content-disposition", "attachment;filename="+fileName);//设置下载的文件名称
|
|
|
+ OutputStream out = null; //创建页面返回方式为输出流,可弹出下载框
|
|
|
+ try {
|
|
|
+ out = res.getOutputStream();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建输入流读取文件
|
|
|
+ InputStream is = new FileInputStream(targetPath);
|
|
|
+ byte[] Buffer = new byte[4096];
|
|
|
+ int size = 0;
|
|
|
+ while((size=is.read(Buffer)) != -1){
|
|
|
+ out.write(Buffer, 0, size);
|
|
|
+ }
|
|
|
+
|
|
|
+ //关闭和释放流
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ is.close();
|
|
|
+ renderNull();
|
|
|
+ }
|
|
|
+
|
|
|
}
|