读文件/写文件。http请求。读取文件列表。

复制代码
package transfor;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class demo {

    private static ArrayList<String> filelist = new ArrayList<String>();
    private static String CHARSET = "utf-8";

    public static void main(String[] args) throws IOException {
         refreshFileList("F:\\11");
         for(int i = 0; i<filelist.size();i++) {
             long a = System.currentTimeMillis();
             System.out.println(filelist.get(i));
             String text ="";

             String wstr = filelist.get(i).replace(".json", "")+".txt";

             text = run(filelist.get(i));

             putconent(text,wstr);

             long b = System.currentTimeMillis();

             System.out.println("程序运行时间: "+(b-a)+"ms");
         }
         filelist.clear();
    }

    public static void refreshFileList(String strPath) {
        File dir = new File(strPath);
        File[] files = dir.listFiles();

        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                refreshFileList(files[i].getAbsolutePath());
            } else {
                String strFileName = files[i].getAbsolutePath().toLowerCase();
                filelist.add(files[i].getAbsolutePath());
            }
        }
    }

    private static void putconent(String content, String filename) {
        try {
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(filename)
                    )
            );
            bw.write(content);
            bw.flush();
            bw.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static String getContent(String filename)throws Exception {
        String ss = "";
        BufferedReader br = new BufferedReader(new InputStreamReader( 
                new FileInputStream(filename), "utf-8")); 
        String line="";
        while ((line = br.readLine()) != null) {
           ss += line;
        }
        br.close();

        return ss;
    }

    public static String run(String filename) {
        String rtn = "";
        try {

            URL url = new URL("http://172.19.34.128:8801/charCorrect");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");//POST
            connection.setDoInput(true);
            connection.setDoOutput(true);
//            connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//            connection.setRequestProperty("content-type", "test/xml");
            connection.setRequestProperty("content-type", "application/json");
            
            connection.connect();

            String con = getContent(filename);

            StringBuffer requestXml = new StringBuffer();
            requestXml.append(con);

            PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), CHARSET));
            try {
                writer.print(requestXml.toString());
                writer.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                writer.close();
            }

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("Error: " + connection.getResponseMessage());
            }
            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), CHARSET));
            StringBuffer rtnBuffer = new StringBuffer();
            try {
                String temp = reader.readLine();
                while (temp != null) {
                    rtnBuffer.append(temp);
                    temp = reader.readLine();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                reader.close();
            }

            rtn = rtnBuffer.toString();

        } catch (Exception e) {
            rtn = "失败";
        }
        try {

            System.out.println("结果:"+rtn);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtn;
    }
}
复制代码

 

posted @   _Meditation  阅读(1642)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示