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; } }