|
@@ -0,0 +1,293 @@
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.util;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
+import com.aliyun.oss.model.PutObjectRequest;
|
|
|
+import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.PaymentManagement;
|
|
|
+import lombok.Getter;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.dom4j.io.OutputFormat;
|
|
|
+import org.dom4j.io.SAXReader;
|
|
|
+import org.dom4j.io.XMLWriter;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.FileSystems;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+public class XmlUtil implements InitializingBean {
|
|
|
+ @Getter
|
|
|
+ @Value("${oss.endpoint.internal:}")
|
|
|
+ private static String baseUrl;
|
|
|
+ @Getter
|
|
|
+ @Value("${oss.bucket.name:}")
|
|
|
+ private static String bucket;
|
|
|
+ @Autowired
|
|
|
+ private static OSSClient ossClient;
|
|
|
+ @Getter
|
|
|
+ @Value("${oss.endpoint.default:}")
|
|
|
+ private static String endpoint;
|
|
|
+
|
|
|
+ private String host;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ host = "http://" + bucket + "." + endpoint;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String xmlChangeString(String fileName) {
|
|
|
+ try {
|
|
|
+ SAXReader saxReader = new SAXReader();//新建一个解析类
|
|
|
+ Document tempDocument = saxReader.read(cn.hutool.core.util.XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
|
|
|
+ return tempDocument.asXML();
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //将字符串string类型转换成xml文件
|
|
|
+ public static String strChangeXML(String str, String name) throws IOException {
|
|
|
+ SAXReader saxReader = new SAXReader();
|
|
|
+ Document document;
|
|
|
+ try {
|
|
|
+ document = saxReader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
|
|
|
+ OutputFormat format = OutputFormat.createPrettyPrint();
|
|
|
+ File file = new File(new String(("templates" + File.separator + name + ".xml").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
|
|
|
+ if (!file.exists()) {
|
|
|
+ boolean flag = file.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());
|
|
|
+
|
|
|
+ MultipartFile multipartFile = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
|
|
|
+ /** 将document中的内容写入文件中 */
|
|
|
+ XMLWriter writer = new XMLWriter(new FileWriter(file), format);
|
|
|
+ writer.write(document);
|
|
|
+ writer.close();
|
|
|
+ String returnFile = upload(multipartFile, name);
|
|
|
+ return returnFile;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "NG";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String readXml(PaymentManagement paymentManagement) {
|
|
|
+ String regEx = "[^0-9]";
|
|
|
+ Pattern p = Pattern.compile(regEx);
|
|
|
+ Matcher m = p.matcher(paymentManagement.getPaymentNo());
|
|
|
+ String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Kp>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Version>2.0</Version>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Fpxx>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Zsl>1</Zsl>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Fpsj>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Fp>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Djh>" + m.replaceAll("").trim() + "</Djh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Gfmc>" + paymentManagement.getCustomerName() + "</Gfmc>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Gfsh>" + paymentManagement.getTaxRegistrationNo() + "</Gfsh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Gfyhzh>" + paymentManagement.getIdentityAuthenticationInfo().getBankDeposit()+paymentManagement.getIdentityAuthenticationInfo().getBankCard() + "</Gfyhzh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Gfdzdh>" + paymentManagement.getIdentityAuthenticationInfo().getCompAddress() + "</Gfdzdh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Bz>车号:" + paymentManagement.getCarNo() + "毛:" + paymentManagement.getGrossWeight() + " 皮:" + paymentManagement.getTare() + " 净:" + paymentManagement.getNetWeight() + " 应付:" + paymentManagement.getGrainMoney() + " 整晒费:0 实付:" + paymentManagement.getActualPayment() + " 性质:贸易粮 单号:" + m.replaceAll("").trim() + " ,★☆性质:贸易粮 形态:玉米堆</Bz>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Fhr/>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Skr/>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Spbmbbh>10.0</Spbmbbh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Hsbz>0</Hsbz>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Spxx>\n" +
|
|
|
+ "\n" +
|
|
|
+ "\n" +
|
|
|
+ "-<Sph>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Xh>1</Xh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Spmc>" + paymentManagement.getGoodsName() + "</Spmc>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Ggxh>" + paymentManagement.getGoodsName() + "</Ggxh>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Jldw>公斤</Jldw>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Spbm>1010101030000000000</Spbm>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Qyspbm/>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Syyhzcbz>0</Syyhzcbz>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Lslbz>1</Lslbz>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Yhzcsm/>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Dj>1.55400000</Dj>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Sl>" + paymentManagement.getNetWeight() + "</Sl>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Je>" + paymentManagement.getGrainMoney() + "</Je>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Kce>0</Kce>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Slv>0</Slv>\n" +
|
|
|
+ "\n" +
|
|
|
+ "<Se>0</Se>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Sph>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Spxx>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Fp>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Fpsj>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Fpxx>\n" +
|
|
|
+ "\n" +
|
|
|
+ "</Kp>";
|
|
|
+ try {
|
|
|
+ String returnFile = strChangeXML(s, paymentManagement.getId());
|
|
|
+ return returnFile;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "NG";
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String args[]){
|
|
|
+// String s ="<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Kp>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Version>2.0</Version>\n" +
|
|
|
+// "\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Fpxx>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Zsl>1</Zsl>\n" +
|
|
|
+// "\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Fpsj>\n" +
|
|
|
+// "\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Fp>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Djh>23003656</Djh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Gfmc>12</Gfmc>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Gfsh>34</Gfsh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Gfyhzh>56</Gfyhzh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Gfdzdh>九台镇南戈庄村186号</Gfdzdh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Bz>车号:鲁VU2873毛:15140 皮:6120 净:9020 应付:14017.08 整晒费:0 实付:14017.08 性质:贸易粮 单号:23003656 ,★☆性质:贸易粮 形态:玉米堆</Bz>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Fhr/>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Skr/>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Spbmbbh>10.0</Spbmbbh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Hsbz>0</Hsbz>\n" +
|
|
|
+// "\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Spxx>\n" +
|
|
|
+// "\n" +
|
|
|
+// "\n" +
|
|
|
+// "-<Sph>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Xh>1</Xh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Spmc>玉米</Spmc>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Ggxh>3等</Ggxh>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Jldw>公斤</Jldw>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Spbm>1010101030000000000</Spbm>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Qyspbm/>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Syyhzcbz>0</Syyhzcbz>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Lslbz>1</Lslbz>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Yhzcsm/>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Dj>1.55400000</Dj>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Sl>9020</Sl>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Je>14017.08</Je>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Kce>0</Kce>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Slv>0</Slv>\n" +
|
|
|
+// "\n" +
|
|
|
+// "<Se>0</Se>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Sph>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Spxx>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Fp>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Fpsj>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Fpxx>\n" +
|
|
|
+// "\n" +
|
|
|
+// "</Kp>";
|
|
|
+// try{
|
|
|
+// strChangeXML(s,"2223");
|
|
|
+// }catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 后台通过服务器间接传文件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static String upload(MultipartFile file, String name) throws IOException {
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ objectMetadata.setContentLength(file.getSize());
|
|
|
+ objectMetadata.setContentType(file.getContentType());
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, "FP/" + name + ".xml", file.getInputStream(), objectMetadata);
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+ return baseUrl + "FP/" + name + ".xml";
|
|
|
+ }
|
|
|
+}
|