|
@@ -0,0 +1,867 @@
|
|
|
+//
|
|
|
+// Source code recreated from a .class file by IntelliJ IDEA
|
|
|
+// (powered by FernFlower decompiler)
|
|
|
+//
|
|
|
+
|
|
|
+package kd.imc.rim.common.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.util.Date;
|
|
|
+import kd.bos.cache.CacheFactory;
|
|
|
+import kd.bos.dataentity.entity.DynamicObject;
|
|
|
+import kd.bos.db.DB;
|
|
|
+import kd.bos.db.DBRoute;
|
|
|
+import kd.bos.db.ResultSetHandler;
|
|
|
+import kd.bos.fileservice.FileService;
|
|
|
+import kd.bos.fileservice.FileServiceFactory;
|
|
|
+import kd.bos.logging.Log;
|
|
|
+import kd.bos.logging.LogFactory;
|
|
|
+import kd.bos.orm.query.QFilter;
|
|
|
+import kd.bos.servicehelper.BusinessDataServiceHelper;
|
|
|
+import kd.bos.servicehelper.QueryServiceHelper;
|
|
|
+import kd.bos.servicehelper.operation.SaveServiceHelper;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+
|
|
|
+public class FileUtilsEx {
|
|
|
+ private static Log logger = LogFactory.getLog(HttpUtil.class);
|
|
|
+ public static final String FILE_TYPE_PDF = "pdf";
|
|
|
+ public static final String FILE_TYPE_OFD = "ofd";
|
|
|
+ public static final String FILE_TYPE_JPG = "jpg";
|
|
|
+ public static final String FILE_TYPE_XML = "xml";
|
|
|
+ private static final String USER_DIR = "user.dir";
|
|
|
+ private static final String FILE_TEMP = "fileTemp";
|
|
|
+ public static final String BASE_UPLOAD_ABS_PATH;
|
|
|
+
|
|
|
+ public FileUtilsEx() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isImage(String fileType) {
|
|
|
+ return "png".equalsIgnoreCase(fileType) || "bmp".equalsIgnoreCase(fileType) || "jpg".equalsIgnoreCase(fileType) || "jpeg".equalsIgnoreCase(fileType) || "webp".equalsIgnoreCase(fileType);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSHA256(InputStream file) {
|
|
|
+ Object var2;
|
|
|
+ try {
|
|
|
+ MessageDigest MD5 = MessageDigest.getInstance("SHA-256");
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+
|
|
|
+ int length;
|
|
|
+ while((length = file.read(buffer)) != -1) {
|
|
|
+ MD5.update(buffer, 0, length);
|
|
|
+ }
|
|
|
+
|
|
|
+ String var4 = new String(Hex.encodeHex(MD5.digest()));
|
|
|
+ return var4;
|
|
|
+ } catch (Exception var14) {
|
|
|
+ var2 = null;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (file != null) {
|
|
|
+ file.close();
|
|
|
+ }
|
|
|
+ } catch (IOException var13) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return (String)var2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String downLoadAndUpload(String fileUrl) {
|
|
|
+ return downLoadAndUpload(fileUrl, (String)null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static InputStream getInputStreamByGet(String url) throws ClientProtocolException, IOException {
|
|
|
+ return getInputStreamByGet(url, 60000, 60000);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static InputStream getInputStreamByGet(String url, int connectionTimeout, int readTimeout) throws ClientProtocolException, IOException {
|
|
|
+ String proxy = HttpUtil.getProxy(url, (String)null);
|
|
|
+ CloseableHttpClient httpclient = HttpUtil.getConnection(proxy, false, connectionTimeout, readTimeout);
|
|
|
+ HttpGet httpGet = HttpUtil.getHttpGet(HttpUtil.replaceUrl(url));
|
|
|
+ CloseableHttpResponse response = httpclient.execute(httpGet);
|
|
|
+ return response.getEntity().getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String downLoadAndUploadTemp(String fileUrl, String fileName) {
|
|
|
+ String tempSavePath = "";
|
|
|
+ if (!StringUtils.isEmpty(fileUrl) && fileUrl.indexOf("http") == 0) {
|
|
|
+ AwsFpyService service = AwsFpyService.newInstance();
|
|
|
+ HttpClient client = HttpUtil.getConnection(service.getProxy(), false);
|
|
|
+ HttpGet get = HttpUtil.getHttpGet(HttpUtil.replaceUrl(fileUrl));
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ response = (CloseableHttpResponse)client.execute(get);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode) {
|
|
|
+ String fileType = "";
|
|
|
+ fileType = getSuffixFromDisposition(response);
|
|
|
+ if (StringUtils.isEmpty(fileType)) {
|
|
|
+ fileType = getSuffixFromContentType(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ fileName = UUID.randomUUIDZero() + '.' + fileType;
|
|
|
+ } else if (fileName.indexOf(46) < 0) {
|
|
|
+ fileName = fileName + '.' + fileType;
|
|
|
+ }
|
|
|
+
|
|
|
+ tempSavePath = CacheFactory.getCommonCacheFactory().getTempFileCache().saveAsUrl(fileName, entity.getContent(), 5000);
|
|
|
+ } else {
|
|
|
+ if (302 == statusCode) {
|
|
|
+ Header header = response.getFirstHeader("location");
|
|
|
+ String newUrl = header.getValue();
|
|
|
+ logger.info("重定向:" + newUrl);
|
|
|
+ String var12 = downLoadAndUpload(newUrl, fileName);
|
|
|
+ return var12;
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.error(String.format("下载文件失败[%s]%s", statusCode, fileUrl));
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info(String.format("下载文件耗时[%s]%s", System.currentTimeMillis() - start, fileUrl));
|
|
|
+ } catch (IOException var28) {
|
|
|
+ logger.error("下载文件失败" + fileUrl, var28);
|
|
|
+ } finally {
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException var27) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ get.abort();
|
|
|
+ get.releaseConnection();
|
|
|
+ } catch (Exception var26) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return tempSavePath;
|
|
|
+ } else {
|
|
|
+ return fileUrl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String downLoadAndUpload(String fileUrl, String fileName) {
|
|
|
+ return (String)downLoadAndUpload(fileUrl, fileName, true, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject downLoadAndUploadWithFileType(String fileUrl, String fileName) {
|
|
|
+ return (JSONObject)downLoadAndUpload(fileUrl, fileName, true, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String downLoadAndUpload(String fileUrl, String fileName, boolean proxy) {
|
|
|
+ return (String)downLoadAndUpload(fileUrl, fileName, proxy, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object downLoadAndUpload(String fileUrl, String fileName, boolean proxy, boolean needFileType) {
|
|
|
+ String tempSavePath = "";
|
|
|
+ String urlFileType = "";
|
|
|
+ if (!StringUtils.isEmpty(fileUrl) && fileUrl.indexOf("http") == 0) {
|
|
|
+ String fileId = MD5.md5Hex(fileUrl);
|
|
|
+ JSONObject pathAndType;
|
|
|
+ if (needFileType) {
|
|
|
+ pathAndType = queryFilePathAndType(fileId);
|
|
|
+ if (pathAndType != null) {
|
|
|
+ return pathAndType;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String path = queryFilePath(fileId);
|
|
|
+ if (!StringUtils.isEmpty(path)) {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pathAndType = null;
|
|
|
+ AwsFpyService service = AwsFpyService.newInstance();
|
|
|
+ CloseableHttpClient client;
|
|
|
+ if (proxy) {
|
|
|
+ client = HttpUtil.getConnection(service.getProxy(), false);
|
|
|
+ } else {
|
|
|
+ client = HttpUtil.getConnection((String)null, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet get = HttpUtil.getHttpGet(HttpUtil.replaceUrl(fileUrl));
|
|
|
+ CloseableHttpResponse response1 = null;
|
|
|
+ CloseableHttpResponse response302 = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ urlFileType = getFileType(fileUrl);
|
|
|
+ if (!StringUtils.isEmpty(urlFileType) && !"do".equals(urlFileType)) {
|
|
|
+ fileName = UUID.randomUUIDZero() + '.' + urlFileType;
|
|
|
+ }
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ response1 = (CloseableHttpResponse)client.execute(get);
|
|
|
+ int statusCode1 = response1.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode1) {
|
|
|
+ Pair<String, String> pair = dealResponse(response1, fileName, fileUrl);
|
|
|
+ tempSavePath = (String)pair.getLeft();
|
|
|
+ if (StringUtils.isNotEmpty((String)pair.getRight())) {
|
|
|
+ urlFileType = (String)pair.getRight();
|
|
|
+ }
|
|
|
+ } else if (302 == statusCode1) {
|
|
|
+ Header header = response1.getFirstHeader("location");
|
|
|
+ String newUrl = header.getValue();
|
|
|
+ logger.info("重定向:" + newUrl);
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ urlFileType = getFileType(newUrl);
|
|
|
+ if (!StringUtils.isEmpty(urlFileType)) {
|
|
|
+ fileName = UUID.randomUUIDZero() + '.' + urlFileType;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet newget = HttpUtil.getHttpGet(HttpUtil.replaceUrl(newUrl));
|
|
|
+ response302 = (CloseableHttpResponse)client.execute(newget);
|
|
|
+ int statusCode2 = response302.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode2) {
|
|
|
+ Pair<String, String> pair = dealResponse(response302, fileName, newUrl);
|
|
|
+ tempSavePath = (String)pair.getLeft();
|
|
|
+ if (StringUtils.isNotEmpty((String)pair.getRight())) {
|
|
|
+ urlFileType = (String)pair.getRight();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("下载文件失败302[{}]{}:{}", new Object[]{statusCode2, newUrl, JSON.toJSONString(response302)});
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("下载文件失败[{}]{}:{}", new Object[]{statusCode1, fileUrl, JSON.toJSONString(response1)});
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("下载文件耗时[{}]{}", System.currentTimeMillis() - start, fileUrl);
|
|
|
+ } catch (IOException var36) {
|
|
|
+ logger.error("下载文件失败" + fileUrl, var36);
|
|
|
+ } finally {
|
|
|
+ if (response1 != null) {
|
|
|
+ try {
|
|
|
+ response1.close();
|
|
|
+ } catch (IOException var35) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response302 != null) {
|
|
|
+ try {
|
|
|
+ response302.close();
|
|
|
+ } catch (IOException var34) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ get.abort();
|
|
|
+ get.releaseConnection();
|
|
|
+ } catch (Exception var33) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(tempSavePath)) {
|
|
|
+ saveFilePath(fileUrl, tempSavePath, fileId, urlFileType);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (needFileType) {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("file_url", tempSavePath);
|
|
|
+ result.put("file_type", urlFileType);
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ return tempSavePath;
|
|
|
+ }
|
|
|
+ } else if (needFileType) {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("file_url", fileUrl);
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ return fileUrl;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pair<String, byte[]> downLoadFileToByte(String fileUrl, String fileName) {
|
|
|
+ return downLoadFileToByte(fileUrl, fileName, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pair<String, byte[]> downLoadFileToByte(String fileUrl, String fileName, boolean proxy) {
|
|
|
+ logger.info("downLoadFileToByte,fileUrl:{}, fileName:{}", fileUrl, fileName);
|
|
|
+ if (!StringUtils.isEmpty(fileUrl) && fileUrl.indexOf("http") == 0) {
|
|
|
+ byte[] fileByte = null;
|
|
|
+ String urlFileType = "";
|
|
|
+ HttpClient client = null;
|
|
|
+ AwsFpyService service = AwsFpyService.newInstance();
|
|
|
+ if (proxy) {
|
|
|
+ client = HttpUtil.getConnection(service.getProxy(), false);
|
|
|
+ } else {
|
|
|
+ client = HttpUtil.getConnection((String)null, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet get = HttpUtil.getHttpGet(HttpUtil.replaceUrl(fileUrl));
|
|
|
+ CloseableHttpResponse response1 = null;
|
|
|
+ CloseableHttpResponse response302 = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ urlFileType = getFileType(fileUrl);
|
|
|
+ if (!StringUtils.isEmpty(urlFileType) && !"do".equals(urlFileType)) {
|
|
|
+ fileName = UUID.randomUUIDZero() + '.' + urlFileType;
|
|
|
+ }
|
|
|
+
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ response1 = (CloseableHttpResponse)client.execute(get);
|
|
|
+ int statusCode1 = response1.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode1) {
|
|
|
+ HttpEntity entity1 = response1.getEntity();
|
|
|
+ fileByte = getByte(entity1.getContent());
|
|
|
+ if (StringUtils.isEmpty(urlFileType)) {
|
|
|
+ urlFileType = getSuffixFromDisposition(response1);
|
|
|
+ }
|
|
|
+ } else if (302 == statusCode1) {
|
|
|
+ Header header = response1.getFirstHeader("location");
|
|
|
+ String newUrl = header.getValue();
|
|
|
+ logger.info("重定向:" + newUrl);
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ urlFileType = getFileType(newUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpGet newget = HttpUtil.getHttpGet(HttpUtil.replaceUrl(newUrl));
|
|
|
+ response302 = (CloseableHttpResponse)client.execute(newget);
|
|
|
+ int statusCode2 = response302.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode2) {
|
|
|
+ HttpEntity entity2 = response302.getEntity();
|
|
|
+ fileByte = getByte(entity2.getContent());
|
|
|
+ if (StringUtils.isEmpty(urlFileType)) {
|
|
|
+ urlFileType = getSuffixFromDisposition(response302);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("下载文件失败302[{}]{}:{}", new Object[]{statusCode2, newUrl, JSON.toJSONString(response302)});
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("下载文件失败[{}]{}:{}", new Object[]{statusCode1, fileUrl, JSON.toJSONString(response1)});
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("下载文件耗时[{}]{}", System.currentTimeMillis() - start, fileUrl);
|
|
|
+ } catch (IOException var34) {
|
|
|
+ logger.error("下载文件失败" + fileUrl, var34);
|
|
|
+ } finally {
|
|
|
+ if (response1 != null) {
|
|
|
+ try {
|
|
|
+ response1.close();
|
|
|
+ } catch (IOException var33) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response302 != null) {
|
|
|
+ try {
|
|
|
+ response302.close();
|
|
|
+ } catch (IOException var32) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ get.abort();
|
|
|
+ get.releaseConnection();
|
|
|
+ } catch (Exception var31) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return Pair.of(urlFileType, fileByte);
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Pair<String, String> dealResponse(CloseableHttpResponse response, String fileName, String fileUrl) throws UnsupportedOperationException, IOException {
|
|
|
+ String fileType = getSuffixFromDisposition(response);
|
|
|
+ if (StringUtils.isEmpty(fileType)) {
|
|
|
+ fileType = getSuffixFromContentType(response);
|
|
|
+ if ("ofd".equals(fileType)) {
|
|
|
+ String fileTypeFromName = getFileType(fileName);
|
|
|
+ if (!StringUtils.isEmpty(fileType)) {
|
|
|
+ fileType = fileTypeFromName;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ fileName = UUID.randomUUIDZero() + '.' + fileType;
|
|
|
+ } else if (fileName.indexOf(46) < 0) {
|
|
|
+ fileName = fileName + '.' + fileType;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(fileType) && fileName.indexOf(46) < 0) {
|
|
|
+ logger.error("文件类型错误取消上传:" + fileUrl);
|
|
|
+ return Pair.of(((Object)null).toString(), fileType);
|
|
|
+ } else {
|
|
|
+ byte[] fileByte = getByte(entity.getContent());
|
|
|
+ ByteArrayInputStream bs = new ByteArrayInputStream(fileByte);
|
|
|
+ Throwable var7 = null;
|
|
|
+
|
|
|
+ Pair var9;
|
|
|
+ try {
|
|
|
+ String url = FileUploadUtils.upload(FileUploadUtils.getInvoiceDir("invoice") + fileName, fileName, bs);
|
|
|
+ var9 = Pair.of(url, fileType);
|
|
|
+ } catch (Throwable var18) {
|
|
|
+ var7 = var18;
|
|
|
+ throw var18;
|
|
|
+ } finally {
|
|
|
+ if (bs != null) {
|
|
|
+ if (var7 != null) {
|
|
|
+ try {
|
|
|
+ bs.close();
|
|
|
+ } catch (Throwable var17) {
|
|
|
+ var7.addSuppressed(var17);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ bs.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return var9;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void saveFilePath(String fileUrl, String attachPath, String fileId, String urlFileType) {
|
|
|
+ try {
|
|
|
+ DynamicObject downResult = BusinessDataServiceHelper.newDynamicObject("rim_download_file");
|
|
|
+ downResult.set("fileid", fileId);
|
|
|
+ downResult.set("http_url", fileUrl);
|
|
|
+ downResult.set("file_url", attachPath);
|
|
|
+ downResult.set("create_time", new Date());
|
|
|
+ downResult.set("file_type", convertFileType(urlFileType));
|
|
|
+ SaveServiceHelper.save(new DynamicObject[]{downResult});
|
|
|
+ } catch (Exception var5) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String convertFileType(String fileType) {
|
|
|
+ logger.info("下载文件类型{}", fileType);
|
|
|
+ if (!"1".equals(fileType) && !"pdf".equals(fileType)) {
|
|
|
+ if (!"4".equals(fileType) && !"ofd".equals(fileType)) {
|
|
|
+ return !"9".equals(fileType) && !"xml".equals(fileType) ? "jpg" : "xml";
|
|
|
+ } else {
|
|
|
+ return "ofd";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return "pdf";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String convertFileTypeNum(String fileType) {
|
|
|
+ if (!"1".equals(fileType) && !"pdf".equals(fileType)) {
|
|
|
+ if (!"4".equals(fileType) && !"ofd".equals(fileType)) {
|
|
|
+ return !"9".equals(fileType) && !"xml".equals(fileType) ? "2" : "9";
|
|
|
+ } else {
|
|
|
+ return "4";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return "1";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String queryFilePath(String fileId) {
|
|
|
+ if (!StringUtils.isEmpty(fileId)) {
|
|
|
+ QFilter filter = new QFilter("fileid", "=", fileId);
|
|
|
+ DynamicObject obj = QueryServiceHelper.queryOne("rim_download_file", "id,file_url", new QFilter[]{filter});
|
|
|
+ if (obj != null && !StringUtils.isEmpty(obj.getString("file_url"))) {
|
|
|
+ return obj.getString("file_url");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject queryFilePathAndType(String fileId) {
|
|
|
+ if (!StringUtils.isEmpty(fileId)) {
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ QFilter filter = new QFilter("fileid", "=", fileId);
|
|
|
+ DynamicObject obj = QueryServiceHelper.queryOne("rim_download_file", "id,file_url,file_type", new QFilter[]{filter});
|
|
|
+ if (obj != null && !StringUtils.isEmpty(obj.getString("file_url"))) {
|
|
|
+ result.put("file_url", obj.getString("file_url"));
|
|
|
+ result.put("file_type", obj.getString("file_type"));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getSuffixFromDisposition(CloseableHttpResponse response) {
|
|
|
+ Header[] headers = response.getHeaders("Content-Disposition");
|
|
|
+ if (headers == null) {
|
|
|
+ headers = response.getHeaders("content-disposition");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (headers != null) {
|
|
|
+ Header[] var2 = headers;
|
|
|
+ int var3 = headers.length;
|
|
|
+
|
|
|
+ for(int var4 = 0; var4 < var3; ++var4) {
|
|
|
+ Header header = var2[var4];
|
|
|
+ String value = header.getValue().replace("\"", "");
|
|
|
+ logger.info("getSuffixFromDisposition headervalue:{}", value);
|
|
|
+ int nameIndex = value.indexOf("filename");
|
|
|
+ if (nameIndex > 0 && value.length() > 10) {
|
|
|
+ String fileName = value.substring(nameIndex + 10, value.length());
|
|
|
+ fileName = fileName.replace(";", "");
|
|
|
+ if (fileName.lastIndexOf(46) > 0) {
|
|
|
+ return fileName.substring(fileName.lastIndexOf(46) + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getSuffixFromContentType(CloseableHttpResponse response) {
|
|
|
+ Header[] headers = response.getHeaders("Content-Type");
|
|
|
+ if (headers != null) {
|
|
|
+ Header[] var2 = headers;
|
|
|
+ int var3 = headers.length;
|
|
|
+
|
|
|
+ for(int var4 = 0; var4 < var3; ++var4) {
|
|
|
+ Header header = var2[var4];
|
|
|
+ if (header.getValue().indexOf("pdf") > 0) {
|
|
|
+ return "pdf";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (header.getValue().indexOf("octet") > 0) {
|
|
|
+ return "ofd";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (header.getValue().indexOf("image/png") >= 0) {
|
|
|
+ return "png";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (header.getValue().indexOf("image/gif") >= 0) {
|
|
|
+ return "gif";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (header.getValue().indexOf("image") >= 0) {
|
|
|
+ return "jpg";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ByteArrayOutputStream cloneInputStream(InputStream input) {
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+
|
|
|
+ int len;
|
|
|
+ while((len = input.read(buffer)) > -1) {
|
|
|
+ baos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+
|
|
|
+ baos.flush();
|
|
|
+ return baos;
|
|
|
+ } catch (IOException var4) {
|
|
|
+ logger.error("获取开票人异常:" + var4.getMessage(), var4);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isExcel(String fileName) {
|
|
|
+ return checkFileType(fileName, "xls", "xlsx");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean checkFileType(String fileName, String... types) {
|
|
|
+ if (fileName != null && types != null && types.length >= 1) {
|
|
|
+ String fileType = getFileType(fileName);
|
|
|
+ if ("".equals(fileType)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ String[] var3 = types;
|
|
|
+ int var4 = types.length;
|
|
|
+
|
|
|
+ for(int var5 = 0; var5 < var4; ++var5) {
|
|
|
+ String str = var3[var5];
|
|
|
+ if (fileType.equalsIgnoreCase(str)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getFileType(String fileName) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileName);
|
|
|
+ return FilenameUtils.getExtension(url.getPath());
|
|
|
+ } catch (MalformedURLException var2) {
|
|
|
+ return FilenameUtils.getExtension(fileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getRealPath(String fileId) {
|
|
|
+ if (StringUtils.isEmpty(fileId)) {
|
|
|
+ return "";
|
|
|
+ } else {
|
|
|
+ String path = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ String sql = "select fpath from t_bas_filepathmapping where FFILEID = ? ";
|
|
|
+ path = (String)DB.query(DBRoute.base, sql, new String[]{fileId}, new ResultSetHandler<String>() {
|
|
|
+ public String handle(ResultSet rs) throws Exception {
|
|
|
+ String path;
|
|
|
+ for(path = ""; rs.next(); path = rs.getString("fpath")) {
|
|
|
+ }
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception var3) {
|
|
|
+ logger.info("查询真实地址失败:" + var3);
|
|
|
+ }
|
|
|
+
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pair<String, byte[]> getFileContent(String fileUrl) {
|
|
|
+ return getFileContent(fileUrl, Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Pair<String, byte[]> getFileContent(String fileUrl, Boolean proxy) {
|
|
|
+ if (StringUtils.isEmpty(fileUrl)) {
|
|
|
+ return null;
|
|
|
+ } else if (fileUrl.indexOf("http") != 0) {
|
|
|
+ FileService service = FileServiceFactory.getAttachmentFileService();
|
|
|
+ if (service.exists(fileUrl)) {
|
|
|
+ String fileType = getFileType(fileUrl);
|
|
|
+ if (StringUtils.isEmpty(fileType)) {
|
|
|
+ String path = getRealPath(fileUrl);
|
|
|
+ fileType = getFileType(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Pair.of(fileType, getByte(service.getInputStream(fileUrl)));
|
|
|
+ } else {
|
|
|
+ logger.info("文件不存在:" + fileUrl);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ AwsFpyService service = AwsFpyService.newInstance();
|
|
|
+ HttpClient client = HttpUtil.getConnection(proxy ? service.getProxy() : null, false);
|
|
|
+ HttpGet get = HttpUtil.getHttpGet(HttpUtil.replaceUrl(fileUrl));
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpResponse response302 = null;
|
|
|
+
|
|
|
+ Pair var10;
|
|
|
+ try {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ response = (CloseableHttpResponse)client.execute(get);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (200 != statusCode) {
|
|
|
+ if (302 == statusCode) {
|
|
|
+ Header header = response.getFirstHeader("location");
|
|
|
+ String newUrl = header.getValue();
|
|
|
+ logger.info("重定向:" + newUrl);
|
|
|
+ HttpGet get302 = HttpUtil.getHttpGet(HttpUtil.replaceUrl(newUrl));
|
|
|
+ response302 = (CloseableHttpResponse)client.execute(get302);
|
|
|
+ int statusCode302 = response302.getStatusLine().getStatusCode();
|
|
|
+ if (200 == statusCode302) {
|
|
|
+ Pair var14 = getContentFromResonse(fileUrl, response302);
|
|
|
+ return var14;
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.error(String.format("下载文件失败302[%s]%s:%s", statusCode, newUrl, JSON.toJSONString(response302)));
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("下载文件耗时[{}]{},{}", new Object[]{System.currentTimeMillis() - start, statusCode, fileUrl});
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ var10 = getContentFromResonse(fileUrl, response);
|
|
|
+ } catch (IOException var38) {
|
|
|
+ logger.error("下载文件失败" + fileUrl, var38);
|
|
|
+ return null;
|
|
|
+ } finally {
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException var37) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response302 != null) {
|
|
|
+ try {
|
|
|
+ response302.close();
|
|
|
+ } catch (IOException var36) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ get.abort();
|
|
|
+ get.releaseConnection();
|
|
|
+ } catch (Exception var35) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return var10;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Pair<String, byte[]> getContentFromResonse(String fileUrl, CloseableHttpResponse response) throws IOException {
|
|
|
+ String fileType = getSuffixFromDisposition(response);
|
|
|
+ if (StringUtils.isEmpty(fileType)) {
|
|
|
+ fileType = getSuffixFromContentType(response);
|
|
|
+ if ("ofd".equals(fileType)) {
|
|
|
+ fileType = getFileType(fileUrl);
|
|
|
+ if (StringUtils.isEmpty(fileType)) {
|
|
|
+ fileType = "ofd";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ return Pair.of(fileType, getByte(entity.getContent()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] getAttachmentByte(String savePath) {
|
|
|
+ FileService service = FileServiceFactory.getAttachmentFileService();
|
|
|
+
|
|
|
+ try {
|
|
|
+ InputStream bos = service.getInputStream(savePath);
|
|
|
+ Throwable var3 = null;
|
|
|
+
|
|
|
+ byte[] var4;
|
|
|
+ try {
|
|
|
+ var4 = getByte(bos);
|
|
|
+ } catch (Throwable var14) {
|
|
|
+ var3 = var14;
|
|
|
+ throw var14;
|
|
|
+ } finally {
|
|
|
+ if (bos != null) {
|
|
|
+ if (var3 != null) {
|
|
|
+ try {
|
|
|
+ bos.close();
|
|
|
+ } catch (Throwable var13) {
|
|
|
+ var3.addSuppressed(var13);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return var4;
|
|
|
+ } catch (IOException var16) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] getByte(InputStream inputStream) {
|
|
|
+ try {
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ Throwable var2 = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+// Boolean len = false;
|
|
|
+ int len;
|
|
|
+ while((len = inputStream.read(buffer)) != -1) {
|
|
|
+ bos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+
|
|
|
+ byte[] var5 = bos.toByteArray();
|
|
|
+ return var5;
|
|
|
+ } catch (Throwable var15) {
|
|
|
+ var2 = var15;
|
|
|
+ throw var15;
|
|
|
+ } finally {
|
|
|
+ if (bos != null) {
|
|
|
+ if (var2 != null) {
|
|
|
+ try {
|
|
|
+ bos.close();
|
|
|
+ } catch (Throwable var14) {
|
|
|
+ var2.addSuppressed(var14);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (IOException var17) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String encodeBase64File(InputStream inputFile) throws Exception {
|
|
|
+ if (inputFile == null) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+// int n = false;
|
|
|
+
|
|
|
+ int n;
|
|
|
+ while(-1 != (n = inputFile.read(buffer))) {
|
|
|
+ output.write(buffer, 0, n);
|
|
|
+ }
|
|
|
+
|
|
|
+ String retStr = Base64.encodeBase64String(output.toByteArray());
|
|
|
+ return retStr;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String truncateFileName(String fileName, int maxLength) {
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
+ return fileName;
|
|
|
+ } else if (fileName.length() <= maxLength) {
|
|
|
+ return fileName;
|
|
|
+ } else {
|
|
|
+ int dotIndex = fileName.lastIndexOf(46);
|
|
|
+ String extension = "";
|
|
|
+ String nameWithoutExtension;
|
|
|
+ if (dotIndex != -1) {
|
|
|
+ nameWithoutExtension = fileName.substring(0, dotIndex);
|
|
|
+ extension = fileName.substring(dotIndex);
|
|
|
+ } else {
|
|
|
+ nameWithoutExtension = fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ int availableLength = maxLength - extension.length();
|
|
|
+ return availableLength <= 0 ? "" : nameWithoutExtension.substring(0, Math.min(availableLength, nameWithoutExtension.length())) + extension;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static {
|
|
|
+ BASE_UPLOAD_ABS_PATH = System.getProperty("user.dir") + File.separator + "fileTemp" + File.separator;
|
|
|
+ }
|
|
|
+}
|