Ecology-Dev-chaofa/盛虹化纤/interface/shhq/TestdownQysfile.jsp

282 lines
12 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="java.nio.charset.Charset" %>
<%@ page import="java.net.HttpURLConnection" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="java.security.MessageDigest" %>
<%@ page import="java.security.NoSuchAlgorithmException" %>
<%@ page import="java.io.*" %>
<%@ page import="java.nio.file.Files" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="java.util.zip.ZipInputStream" %>
<%@ page import="java.util.zip.ZipEntry" %>
<%@ page import="weaver.docs.docs.DocManager" %>
<%@ page import="weaver.docs.docs.ImageFileIdUpdate" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.docs.docs.DocImageManager" %>
<%@ page import="weaver.conn.RecordSet" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page" />
<%
int sum = 0;
String SERVICE_URL = "http://172.30.18.85:9182";
String APP_TOKEN = "LyLs17DTjG";
String APP_SECRET = "omiAv7FEEbVpPlqhl7YeJRSCAGa93I";
Charset CHARSET = StandardCharsets.UTF_8;
String DEFAULT_CONTENT_TYPE = "application/octet-stream";
int statusCode = 0;
String urlStr = SERVICE_URL + "/contract/download";
HttpURLConnection conn = null;
OutputStream outt = null;
long timestamp = System.currentTimeMillis();
try {
StringBuilder urlBuilder = new StringBuilder(urlStr);
urlBuilder.append("?");
urlBuilder.append("contractId").append("=").append(URLEncoder.encode("3329734888193044834", "UTF-8"));
urlBuilder.append("&");
urlBuilder.append("downloadItems").append("=").append(URLEncoder.encode("NORMAL", "UTF-8"));
urlBuilder.append("&");
urlBuilder.append("needCompressForOneFile").append("=").append(URLEncoder.encode("false", "UTF-8"));
rs.writeLog(" urlParameters : " + urlBuilder.toString());
URL url = new URL(urlBuilder.toString());
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Host", url.getHost());
conn.setRequestProperty("Accept", "text/plain,application/json");
conn.setRequestProperty("User-Agent", "privateapp-java-api-client");
conn.setRequestProperty("x-qys-accesstoken", APP_TOKEN);
conn.setRequestProperty("x-qys-timestamp", String.valueOf(timestamp));
conn.setRequestProperty("x-qys-signature", encryptMD5(APP_TOKEN+APP_SECRET+timestamp));
conn.setRequestProperty("Content-Type", "");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
String curryear = TimeUtil.getCurrentDateString();
String ym = curryear.replaceAll("-", "").substring(0, 6);
char paramChar = (char) (Math.random() * 26 + 'A');
String path = "/usr/weaver/ecology/filesystem/" + ym + "/" + String.valueOf(paramChar);
File filee = new File(path);
if (!filee.exists()) {
filee.mkdirs();
}
String newname = "aaa.zip";
String filepath = path + "/" + newname;
String currentdate = TimeUtil.getCurrentDateString();
String currenttime = TimeUtil.getOnlyCurrentTimeString();
statusCode = conn.getResponseCode();
rs.writeLog(statusCode);
StringBuilder response1 = new StringBuilder();
if (statusCode == 200) {
try (InputStream ins = conn.getInputStream()){
//process response data
File file = inputStreamToFile(filepath, ins);
try {
byte[] bytes = Files.readAllBytes(file.toPath());
Charset charset = Charset.defaultCharset();
System.out.println("File encoding: " + charset.displayName());
} catch (Exception e) {
e.printStackTrace();
}
rs.writeLog("============== file length ==================== " + file.length());
rs.writeLog("============== file name ==================== " + file.getName());
rs.writeLog("============== file path ==================== " + file.getPath());
FileInputStream fis = new FileInputStream(filepath);
ZipInputStream zis = new ZipInputStream(fis,Charset.forName("GBK"));
byte[] buffer = new byte[1024];
ZipEntry ze = zis.getNextEntry();
int index = 0;
while (ze != null) {
if (!ze.isDirectory()) {
String fileName = ze.getName();
rs.writeLog("============== file fileName ==================== " + fileName);
File newFile = new File(path + "/" + fileName);
// 创建新文件所需的任何不存在的父文件夹
new File(newFile.getParent()).mkdirs();
// 写入当前条目数据到新文件
try (OutputStream fos = new FileOutputStream(newFile)) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
DocManager docManager = new DocManager();
int docid = docManager.getNextDocId(rs);
ImageFileIdUpdate im = new ImageFileIdUpdate();
int imagefileid = im.getImageFileNewId();
String aescode = Util.getRandomString(13);
/* 插入文档信息表 start */
rs.execute("insert into docdetail(id,seccategory,doctype,doclangurage,docreplyable,isreply,replydocid,docsubject,itemid,itemmaincategoryid,financeid,doccreaterid,docdepartmentid,doccreatedate,doccreatetime," +
"doclastmoduserid,doclastmoddate,doclastmodtime,docapproveuserid,docarchiveuserid,ownerid,accessorycount,replaydoccount,usertype,ishistory,maindoc,readoptercanprint,docvaliduserid,docvaliddate,docvalidtime,ownertype,docstatus) " +
" values(" + docid + "," + zzzs_categoryid + ",'1','7','1','0','0','" + fileName + "','0','0','0'," + userid + "," + deptid + ",'" + currentdate + "','" + currenttime + "'," +
"" + userid + ",'" + currentdate + "','" + currenttime + "','0','0','" + userid + "','1','0','1','0'," + docid + ",'0'," + userid + ",'" + currentdate + "','" + currenttime + "','1','1')");
/* 插入文档信息表 end */
rs.execute("insert into imagefile(imagefileid,imagefilename,imagefileused,filerealpath,iszip,isencrypt,filesize,downloads,isaesencrypt,aescode) " +
" values(" + imagefileid + ",'" + fileName + "','1','" + filepath + "','0','0','" + newFile.length() + "','0','0','" + aescode + "')");
/* 插入 文档附件关联表 docimagefile start */
DocImageManager docImageManager = new DocImageManager();
int docimageid = docImageManager.getNextDocImageFileId();
int versionid = getVersionid(); //唯一标识
rs.execute("insert into docimagefile(id,docid,imagefileid,imagefilename,imagefilewidth,imagefileheight,imagefielsize,docfiletype,versionid) " +
" values(" + docimageid + "," + docid + "," + imagefileid + ",'" + fileName + "','0','0','0','2'," + versionid + ")");
/* 插入 文档附件关联表 docimagefile end */
/* 插入文档内容 start */
rs.execute("insert into docdetailcontent(docid,doccontent) values('" + docid + "','')");
/* 插入文档内容 end */
/* 插入文档共享信息表 shareinnerdoc start */
rs.execute("insert into shareinnerdoc(sourceid,type,content,seclevel,sharelevel,srcfrom,opuser,sharesource,downloadlevel,seclevelmax) " +
"values(" + docid + ",'1'," + userid + ",'0','3','80'," + userid + ",'0','1','255')");
/* 插入文档共享信息表 shareinnerdoc end */
/* 插入文档共享信息表 docshare start */
rs.execute("insert into docshare(docid,sharetype,sharelevel,userid,isSecDefaultShare,downloadlevel,seclevelmax) " +
" values(" + docid + ",'80','3'," + userid + ",'1','1','255')");
/* 插入文档共享信息表 docshare end */
}
// 关闭当前ZipEntry并移至下一个
zis.closeEntry();
ze = zis.getNextEntry();
index ++;
rs.writeLog("============== file index ==================== " + index);
}
} catch (Exception e){
// 处理异常情况
}
} else {
// 处理异常情况
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.disconnect();
}
if (outt != null) {
outt.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
sum++;
%>
<%!
/**
* 根据 inputstream 生成文件
* @param newpath
* @param inputStream
* @return
*/
public static File inputStreamToFile(String newpath,InputStream inputStream){
File newFile = new File(newpath);
int bufferSize = 1024; // 缓冲区大小
new BaseBean().writeLog("============== inputStreamToFile newpath ==================== "+ newpath);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// 创建FileOutputStream来写入文件
FileOutputStream fileOutputStream = new FileOutputStream(newpath);
// 缓冲区用于读写数据
byte[] buffer = new byte[bufferSize];
int bytesRead;
// 读取数据并写入文件
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
// 关闭流以释放资源
fileOutputStream.close();
bufferedInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
new BaseBean().writeLog("============== inputStreamToFile newFile length ==================== "+ newFile.length());
return newFile;
}
/**
* MD5 加密
* @param input
* @return
*/
public static String encryptMD5(String input) {
try {
// 创建MessageDigest对象参数为算法名称
MessageDigest md = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
md.update(input.getBytes());
// 完成哈希计算,得到结果
byte[] digest = md.digest();
// 将结果转换为十六进制字符串
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b)); // 将每个字节转换为两位的十六进制数不足两位前面补0
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // 如果MD5算法不可用抛出运行时异常
}
}
/**
* 获取唯一标识
* @return
*/
public int getVersionid(){
int versionid = 0;
RecordSet rs = new RecordSet();
rs.execute("select max(versionid)as versionid from docimagefile");
rs.next();
versionid = Util.getIntValue(rs.getString("versionid"));
versionid = versionid + 1;
return versionid;
}
%>
<HEAD>
</HEAD>
<BODY>
<h1>Congratulation Mode 666666 !</h1>
<h1> sum <%=sum%> </h1>
</BODY>