java使用代理发post请求

这东西啊,本身是无用的,但是要是移植就有用。

package util;
import java.util.Properties; public class HttpProxyConfiger {
public static void configProxy(){
Properties prop=System.getProperties(); prop.setProperty("proxySet","true");
prop.setProperty("http.proxyHost","192.168.xx.xx");
prop.setProperty("http.proxyPort","port"); }
}

工具类中包括很多种方法,读写txt,发post请求等。

package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.lang.StringEscapeUtils; import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.org.apache.bcel.internal.generic.NEW; import dao.DataBaseDao;
import dao.impl.DataBaseDaoImpl;
import entity.AiDataBase;
import entity.AiResultBai;
import entity.AiResultFs; public class Tools {
static Date datetime=new Date();
static Timestamp ts = new Timestamp(datetime.getTime());
//读取本地文件pos.txt
public static List<String> readFile02(String path) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<String> list = new ArrayList<String>();
try {
FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
list.add(line);
}
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* 分析fs数据结果时用
* @param path
* @return
*/
public static List<String> readFiletofs(String path) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<String> list = new ArrayList<String>();
try {
FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
list.add(line.replace("+", "%2B"));
}
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
} /*
* 专门给百度去用的,把一份大文件拆分成几个小文件
* 本地读取13W基础数据,拆分成每1W条存入一个list执行一次分析
*/
public static Map readFilechaifen(String path) {
Map map=new HashMap();
//存放总数的text
List<String> listall=new ArrayList<String>();
try { FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
listall.add(line.replace("+", "%2B"));
}
br.close();
isr.close();
fis.close(); } catch (Exception e) {
e.printStackTrace();
}
//每读取一样,index加一,到了index % 5000 == 0 的时候,就用一个新的list
//把if里面的代码放到后面来,list定义在for外面, 在你这个if里面再次new一下
//就好了
List<String> listnew=null;
for(int i=0;i<=listall.size();i++) {
if(i%5000==0) {
listnew=new ArrayList<String>();
listnew.add(listall.get(i)); }
listnew.add(listall.get(i));
}
return map;
} public static List<AiDataBase> readFiletoadb(String path) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<AiDataBase> adblist = new ArrayList<AiDataBase>();
try {
FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
//int i=0;
while ((line = br.readLine()) != null) { //i++;
//System.out.println(i+"==========="+line);
// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
if(line.contains("+")) {
line=line.replace("+", "%2B");
//System.out.println("替换+后=="+line);
}
if(line.contains("'")) {
line=line.replaceAll("'", "");
//System.out.println("替换'后=="+line);
}
if(line.contains("\\")){
line=line.replace("\\", "");
}
AiDataBase aiDataBase=new AiDataBase();
aiDataBase.setText(line.replaceAll(" ", "").trim());
aiDataBase.setCreate_time(ts);
adblist.add(aiDataBase);
} br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return adblist;
} /*public static void main(String[] args) {
List<AiDataBase> adblist =readFiletoadb("E:\\360downloads\\111\\data_jd.txt");
System.out.println(adblist.size());
}*/
//读取本地文件result_fs.txt
public static List<AiResultFs> readFileforFs(String path) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<AiResultFs> list = new ArrayList<AiResultFs>();
try {
FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
DataBaseDao dbd=new DataBaseDaoImpl();
String line = "";
int j=0;
while ((line = br.readLine())!=null) {
if(line.contains("…")) {
line=StringEscapeUtils.escapeHtml(line);
}
if(line.contains("+")) {
line=line.replace("+", "%2B");
//System.out.println("替换+后=="+line);
}
/*if(line.contains("'")) {
line=line.replaceAll("'", "");
//System.out.println("替换'后=="+line);
}*/
j++; if((dbd.getIdByText1(line.split("===")[0].trim().replaceAll(" ", "")))>0){
AiResultFs arf=new AiResultFs();
System.out.println(j+line);
arf.setData_base_id(dbd.getIdByText1(line.split("===")[0].trim().replaceAll(" ", "")));
arf.setSentiment(Integer.parseInt(((((line.split("==="))[1]).split(":"))[1])));
arf.setScore(Double.parseDouble(((((line.split("==="))[2]).split(":"))[1])));
arf.setCreate_time(ts);
list.add(arf);
}else {
System.out.println(line+"不存在");
} //} }
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
} return list;
}
/**
* 给aibaidu.java用的,把13W的数据拆成5000一个list然后进行分析
* @param paramlist
* @return
*/
public static Map<Integer, List<String>> getmapchafen(List<String> paramlist){
List<String> listnew=new ArrayList<String>();
Map<Integer , List<String>> lMap=new HashMap<Integer , List<String>>();
for(int i=0;i<paramlist.size();i++) {
if(i==0 ||i%5000==0) {
listnew=new ArrayList<String>();
listnew.add(paramlist.get(i));
lMap.put(i+1, listnew); }else {
//listnew=new ArrayList<String>();
listnew.add(paramlist.get(i));
} }
System.out.println(lMap.size());
return lMap;
}
//读取本地文件result_baidu.txt,存储为List<AiResultBai>
public static List<AiResultBai> readFileforbaidu(String path) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<AiResultBai> list = new ArrayList<AiResultBai>();
try {
FileInputStream fis = new FileInputStream(path);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
DataBaseDao dbd=new DataBaseDaoImpl();
String line = "";
int j=0;
while ((line = br.readLine())!=null) {
if(!line.equals("")) {
if(line.length()>=256) {
line.substring(0, 256);
}
if(line.contains("+")) {
line=line.replace("+", "%2B");
//System.out.println("替换+后=="+line);
}
/*if(line.contains("'")) {
line=line.replaceAll("'", "''");
//System.out.println("替换'后=="+line);
}*/
/*if(line.contains("…")) {
line=StringEscapeUtils.escapeHtml(line);
}*/
j++;
System.out.println(j+line);
;
AiResultBai arBai=new AiResultBai();
if((dbd.getIdByText1(((line.split("===")[0]).trim().replaceAll(" ", ""))))>0) {
arBai.setData_base_id(dbd.getIdByText1((line.split("===")[0]).trim().replaceAll(" ", "")));
arBai.setSentiment(Integer.parseInt(((((line.split("==="))[1]).split(":"))[1])));
arBai.setPositive_prob(Double.parseDouble(((((line.split("==="))[2]).split(":"))[1])));
arBai.setNegative_prob(Double.parseDouble(((((line.split("==="))[3]).split(":"))[1])));
arBai.setCreate_time(ts);
list.add(arBai);
}else {
System.out.println(line+"不存在");
} } }
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
} return list;
} /*public static void main(String[] args) {
String string="物流很快,手机壳也很精美,自己真的超喜欢啊……但河马嘴巴上的灰渍是脏";
StringEscapeUtils.escapeHtml(string);
System.out.println(string);
//System.out.println(list.size());
}*/
//把字符串一行行写入文件
public void writeTxt(String result,String resultfilepath) {
//写入中文字符时解决中文乱码问题
try {
FileOutputStream fos = new FileOutputStream(new File(resultfilepath),true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(result + "\t\n"); //注意关闭的先后顺序,先打开的后关闭,后打开的先关闭
bw.close();
osw.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//2个文本文件一行一行对比
public static List<String> compare(String path1,String path2) {
// 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
List<String> strlist = new ArrayList<String>();
try {
FileInputStream fis = new FileInputStream(path1);
// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr); FileInputStream fis1 = new FileInputStream(path2);
InputStreamReader isr1 = new InputStreamReader(fis1, "UTF-8");
BufferedReader br1 = new BufferedReader(isr1);
String line1 = "";
String line2="";
while (((line1 = br.readLine()) != null)&&((line2 = br1.readLine()) != null)) {
// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
if(!((line1.trim().replace(" ", "")).equals(line2.trim().replace(" ", "")))) {
strlist.add((line1.split("==="))[0].trim()+"=="+(line1.split("==="))[1]+"=="+(line2.split("==="))[1]);
} }
br.close();
isr.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return strlist;
}
//post请求获取结果
/**
*  sendUrl (远程请求的URL)
*  param (远程请求参数)
*  JSONObject (远程请求返回的JSON)
*/ public String sendPostUrl(String url,String param){
PrintWriter out = null;
BufferedReader in = null;
Gson gson = new Gson();
JsonParser parser=new JsonParser();
String result = "";
List<String> reslutlist=new ArrayList<String>();
String qingganqingxiang="";
String jieguo="";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流(设置请求编码为UTF-8)
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 获取请求返回数据(设置返回数据编码为UTF-8)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
JsonObject jsonObject = parser.parse(result).getAsJsonObject();
// System.out.println(jsonObject);
//分析结果,得出字符串 装了好几台机子了,一直都用这个===情感倾向:2===分值:0.632563
if((Double.parseDouble(jsonObject.get("score").toString()))>0.5){
qingganqingxiang="===2";
}
else {
qingganqingxiang="===0";
}
String score="==="+jsonObject.get("score").toString();
String sentence=jsonObject.get("sentence").toString();
System.out.println("json获取的值"+sentence);
jieguo=sentence+qingganqingxiang+score;
// jieguo=param+qingganqingxiang+score;
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
} return jieguo;
}
/**
* 改变post方法,最终获取的是一个list<airesultfs>
* @param url
* @param paramlist
* @return
*/
public static List<AiResultFs> sendPostUrl1(String url,List<String> paramlist){
Date datetime=new Date();
Timestamp ts = new Timestamp(datetime.getTime());
PrintWriter out = null;
BufferedReader in = null;
Gson gson = new Gson();
JsonParser parser=new JsonParser();
DataBaseDao dBaseDao=new DataBaseDaoImpl(); List<AiResultFs> reslutlist=new ArrayList<AiResultFs>();
for (String param : paramlist) {
try {
HttpProxyConfiger.configProxy();
//System.out.println(param);
URL realUrl = new URL(url);
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
//URLConnection conn = realUrl.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流(设置请求编码为UTF-8)
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
// 发送请求参数
out.print("sentence="+param);
// flush输出流的缓冲
out.flush();
// 获取请求返回数据(设置返回数据编码为UTF-8)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
String result = "";
while ((line = in.readLine()) != null) {
result += line;
} JsonObject jsonObject =parser.parse(result.replace("'", "''")).getAsJsonObject();
//System.out.println("jsonObject=="+jsonObject);
//分析结果,得出字符串 装了好几台机子了,一直都用这个===情感倾向:2===分值:0.632563
int sentiment;
if((Double.parseDouble(jsonObject.get("score").toString()))>0.5){
sentiment=2;
}
else {
sentiment=0;
}
double score=Double.parseDouble(jsonObject.get("score").toString());
String sentence=jsonObject.get("sentence").toString();
System.out.println("param========"+param);
int database_id=dBaseDao.getIdByText1(param.trim().replace(" ", ""));
System.out.println(database_id);
if(database_id>0) {
AiResultFs arf=new AiResultFs();
arf.setData_base_id(database_id);
arf.setSentiment(sentiment);
arf.setScore(score);
arf.setCreate_time(ts);
reslutlist.add(arf);
}
//System.out.println("结束");
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
} return reslutlist;
} }
上一篇:spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常


下一篇:出现No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here异常