Browse Source

青涩知夏->增加文件生成

青涩知夏 4 years ago
parent
commit
67d1985210

+ 3 - 1
src/main/java/cn/nosum/common/http/entity/Request.java

@@ -22,6 +22,7 @@ public class Request {
 
     private Map<String, String> paramMap;
 
+
     public Request(FullHttpRequest req) {
         this.req = req;
         initParam(req);
@@ -54,7 +55,7 @@ public class Request {
     }
 
     public String getUrl() {
-        return req.uri();
+        return this.req.uri().split("[?]")[0];
     }
 
     public String getMethod() {
@@ -68,4 +69,5 @@ public class Request {
     public String getParameter(String name) {
         return paramMap.get(name);
     }
+
 }

+ 107 - 0
src/main/java/cn/nosum/common/util/NettyFileUtil.java

@@ -0,0 +1,107 @@
+package cn.nosum.common.util;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+public class NettyFileUtil {
+
+    private static final Logger logger = LoggerFactory.getLogger(NettyFileUtil.class);
+
+    private final static Integer BYTE_BUFFER_LENGTH=1024;
+
+    /**
+     * 将内存中的数据写入到磁盘,如果路径不存在则创建
+     * @param data 文件内容
+     * @param filePath 文件保存路劲
+     */
+    public static void dataToFile(String data, String filePath) {
+        System.err.println(filePath);
+        File file = new File(filePath);
+        if (!file.getParentFile().exists()) {
+            file.getParentFile().mkdirs();
+        }
+        try (FileOutputStream fileOutputStream = new FileOutputStream(file);
+             FileChannel fileChannel = fileOutputStream.getChannel()) {
+            ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_LENGTH);
+            int i = 0;
+            int length = data.getBytes().length;
+            // 一次性读取完毕
+            if (BYTE_BUFFER_LENGTH > data.getBytes().length) {
+                byteBuffer.put(data.getBytes(), i, data.getBytes().length);
+                byteBuffer.flip();
+                fileChannel.write(byteBuffer);
+            } else {
+                // 循环读取
+                for (int temp = 0; temp < data.getBytes().length; temp += BYTE_BUFFER_LENGTH) {
+                    byteBuffer.clear();
+                    byteBuffer.put(data.getBytes(), temp, BYTE_BUFFER_LENGTH);
+                    byteBuffer.flip();
+                    fileChannel.write(byteBuffer);
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 读取文件信息
+     * @param filePath 目标文件路径
+     * @return 文件内容
+     */
+    public static String dataFromFile(String filePath) {
+        File file = new File(filePath);
+        // 从输入流中获取 channel
+        try (FileInputStream fileInputStream = new FileInputStream(file);
+             FileChannel channel = fileInputStream.getChannel()) {
+
+            // 分配缓冲区
+            ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_LENGTH);
+            StringBuilder result = new StringBuilder();
+            while (true) {
+                byteBuffer.clear();
+                // 将 channel数据写到buffer中
+                int read = channel.read(byteBuffer);
+                // 因为byteBuffer大小原因,因此需要用一个中间字符串接受一下
+                result.append(new String(byteBuffer.array()));
+                if (read == -1) {
+                    break;
+                }
+            }
+            return result.toString();
+        } catch (Exception e) {
+            logger.error("文件读取错误,错误原因 :{0}", e);
+        }
+        return null;
+    }
+
+    /**
+     * 文件复制
+     * @param sourceFilePath 源路径
+     * @param targetFilePath 目标路劲
+     */
+    public static void copyFileUseChannelTransfer(String sourceFilePath, String targetFilePath) {
+        File source = new File(sourceFilePath);
+        File target = new File(targetFilePath);
+        try (FileInputStream fileInputStream = new FileInputStream(source);
+             FileOutputStream fileOutputStream = new FileOutputStream(target);
+             FileChannel fileInputStreamChannel = fileInputStream.getChannel();
+             FileChannel fileOutputStreamChannel = fileOutputStream.getChannel()) {
+            // 直接将输入channel复制到输出channel
+            fileOutputStreamChannel.transferFrom(
+                    fileInputStreamChannel,
+                    fileInputStreamChannel.position(),
+                    fileInputStreamChannel.size()
+            );
+
+        } catch (Exception e) {
+            logger.error("文件复制错误,错误原因 :{0}", e);
+        }
+    }
+}

+ 2 - 0
src/main/java/cn/nosum/gateway/chain/DefaultSlotChainBuilder.java

@@ -1,5 +1,6 @@
 package cn.nosum.gateway.chain;
 
+import cn.nosum.gateway.slot.FileProcessorSlotChain;
 import cn.nosum.gateway.slot.LogProcessorSlotChain;
 import cn.nosum.gateway.slot.UrlProcessorSlotChain;
 
@@ -9,6 +10,7 @@ public class DefaultSlotChainBuilder implements SlotChainBuilder {
         ProcessorSlotChain chain = new DefaultProcessorSlotChain();
         chain.addLast(new UrlProcessorSlotChain());
         chain.addLast(new LogProcessorSlotChain());
+        chain.addLast(new FileProcessorSlotChain());
         return chain;
     }
 }

+ 3 - 0
src/main/java/cn/nosum/gateway/slot/FileProcessorSlotChain.java

@@ -1,7 +1,9 @@
 package cn.nosum.gateway.slot;
 
 import cn.nosum.common.http.entity.Context;
+import cn.nosum.common.util.NettyFileUtil;
 import cn.nosum.gateway.chain.AbstractLinkedProcessorSlot;
+import com.alibaba.fastjson.JSON;
 
 /**
  * 创建文件,进行IO操作
@@ -9,6 +11,7 @@ import cn.nosum.gateway.chain.AbstractLinkedProcessorSlot;
 public class FileProcessorSlotChain extends AbstractLinkedProcessorSlot<Context> {
     @Override
     public void exec(Context context) throws Throwable {
+        NettyFileUtil.dataToFile(JSON.toJSONString(context.getRequest().getParameters()),context.getRequest().getUrl());
         fireExec(context);
     }
 }