读文件/写文件。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;
}
}
上一篇:分享8款最新HTML5/CSS3功能插件及源码下载


下一篇:照片管家iOS-实现本地相册、视频、安全保护、社交分享源码下载Demo