• 读文件/写文件。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;
        }
    }
  • 相关阅读:
    如何用密码保护共享文件?
    如何让光驱自动弹出和关闭?
    欢迎参加“诊断 Windows 7 启动及登录缓慢问题”在线技术会议
    谁偷走了我的系统资源?
    修改Windows验证的登陆框为页面
    使用Fiddler提高前端工作效率
    对非管理员隐藏Site Actions的菜单
    C#多线程参数传递
    转:管理网站集所使用的内容数据库
    转:十步骤让你轻松提升SharePoint性能
  • 原文地址:https://www.cnblogs.com/dhName/p/10620968.html
Copyright © 2020-2023  润新知