- 浏览: 1119042 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1055)
- quartz定时任务 (1)
- json (9)
- 接口 (1)
- http (1)
- sccket (2)
- xmlbean (1)
- webservice (1)
- flex (1)
- sitemesh (2)
- json组装 (2)
- 存储过程 (6)
- ajax后台返回前台上下问 (3)
- flect反射机制 (1)
- 页面列表 (1)
- oracal结果集合并 (1)
- lucene (1)
- 读取配置文件工具 (1)
- 框架整合 (1)
- jms (3)
- 断点续传 (1)
- 页面之间的传值 (1)
- jpa (6)
- sql (8)
- jsp跳转 (1)
- jsp (6)
- jquery (2)
- ui (1)
- js与jsp之间的值的交互应用 (1)
- js空值判断 (1)
- Ajax (9)
- web-inf 编译目录 (1)
- 多线程 (9)
- 资料 (0)
- oracal (4)
- 多线程webservice (1)
- ztree (1)
- 加载与内容的变化 (1)
- 页面的公用变量 (1)
- web-inf目录结构 (1)
- 登陆系列 (1)
- jass应用于webservice认证 (2)
- 路径问题 (1)
- list合并 (1)
- list map中的value获取 (2)
- 标签tag (4)
- 方法 (1)
- 树形数据及其渲染 (1)
- require js 回调 (1)
- tab 页面切换 (2)
- 计时获取验证码 (1)
- 模式 (6)
- easyui (3)
- dialog (2)
- 乱码问题 (1)
- spring (29)
- sqlserver (1)
- iis,netframework (0)
- iis (1)
- netframework (1)
- 处理乱码问题 (1)
- struts-ajax (0)
- ,理论,了;,; (0)
- 排错方法 (1)
- hibernate (1)
- 二级缓存 (1)
- portal (1)
- date类型 (0)
- jvm out of memery StackOverflowError (1)
- echart数据填充 (0)
- echart (1)
- jmv (1)
- 学习 (0)
- java基础 (1)
- oracle性能优化 (1)
- 数据库三范式 (1)
- easy UI (1)
- oracle11g (1)
- form (3)
- servlet (2)
- spring mvc (1)
- htm5 (0)
- ibatise (1)
- 序列化 (1)
- tomcat双认证 (1)
- maven Lib (1)
- tomcat eclipse (2)
- ehcache (1)
- http 代替 ajax (2)
- rest (6)
- script脚本占位模板 (1)
- strust 标签 el表达式 (1)
- js bo (1)
- 公司资料 (0)
- js return (1)
- spring 第三方插件的工具类 (1)
- linux (10)
- linux 克隆 (1)
- linux xshell连接 (1)
- yum expect (1)
- linux 应用命令 (1)
- yum 缓存rmp包 (1)
- yum 本地库离线安装 (2)
- yum错误 (1)
- jsp页面用定时调用 (1)
- ajax setup (2)
- Exception,RuntimeException (1)
- 对话框 dialog (1)
- maven (17)
- PropertyPlaceholderConfigurer properties文件 (1)
- 错误集 (1)
- mybatise (0)
- 拦截器filter (1)
- http get post 区别 (1)
- rest 客户端两种返回的数据处理方式 (1)
- listener (2)
- maven工程 (1)
- ContainerResponseFilter (1)
- ajax action 打印任意实体类jsp (1)
- yum svn mvn (1)
- 分页 (1)
- angularjs (1)
- memcached (1)
- git (12)
- yyyyyyyy (0)
- oracal安装 (4)
- jaxwswebservice (1)
- spring bean (1)
- 排方法 (0)
- java 生成pdf (2)
- 创建目录 (1)
- 下载与生产pdf文件 (1)
- base64 (2)
- 配置文件 (2)
- blob (1)
- pdf (1)
- yum linux 安装Oracle (1)
- excl 插入数据库 (1)
- 搭建框架 (1)
- .classpath和jdk jre (1)
- mybatise 配置文件通配 (1)
- 用Maven插件生成Mybatis代码 (2)
- springmvc值的传递 (1)
- js jsp html (1)
- ajax跨域 (1)
- javamail (0)
- 提交复杂格式json数据 (1)
- get中文乱码/post (1)
- 弹出框,承载弹出链接----页面,显示大图 (1)
- @RequestBody list bean (1)
- 隐藏域 (1)
- 多页保存 (1)
- form 校验 (1)
- 注册,上传图片 (1)
- 登录验证 (1)
- 全局的异常处理 (0)
- 异常捕获,显示在页面 (0)
- mybatise事物配置 (1)
- maven compile (1)
- 时间格式 (1)
- js 跳转页面 (1)
- pringsecurity 角色授资源要重启服务 (0)
- Retrofi restfull (1)
- session过期推出,直接点击退出,退出 (0)
- 参数放于requestbody (1)
- maven本地仓库jar包 (1)
- 编译环境出错jdk1.6 1,7 (1)
- jsonarray ---json数组格式 (1)
- java后台文件上传,接受的2种方式 (0)
- 生成保存图片 (1)
- 权限mysql数据库Md5加密sql (1)
- mysql一对多关联查询 (1)
- mysql批量更新 (0)
- oracle in exists 区别 (1)
- https (1)
- linux发布脚本 (1)
- dubbo zookeepr (1)
- request.getParameter() (1)
- request.setAttribute() (1)
- @RequestMapping @ResponseBody (1)
- @RequestMapping @responsebody src控件 后台如何写入src值 (1)
- dwz (8)
- lib jar maven (1)
- web服务访问名称 (1)
- mvc返回内容设置,拦截 (1)
- @ResponseBody 返回json处理 (1)
- 视图,函数,存储过程 (1)
- <context:annotation-config/> (1)
- <context:component-scan/> (1)
- xml路劲通配 (2)
- 杂项 (1)
- dubbo (34)
- redis (25)
- mybatis (34)
- springmvc (15)
- js (7)
- sprinvmvc (1)
- 设计理念 (1)
- 工程 (1)
- 其他 (149)
- 导出,下载 (1)
- session (4)
- token (1)
- Exception处理 (2)
- 注解 (2)
- 框架 (3)
- shrio (1)
- login (1)
- 项目结构设计 (1)
- mybatis两种关联查询 (1)
- mybaits (2)
- 迭代模式 (1)
- oralce树形数据结构构建 (2)
- 树形结构的jsp展现 (1)
- filter intecept(Struts) (1)
- 树形结构数据的提交 (1)
- @RequestBody @RequestParam (1)
- div (1)
- equals重写 (1)
- 程序设计 (1)
- f多层for循环跳出 (1)
- 看源码 (0)
- 权限系统普遍通性 (1)
- 设计程序 (1)
- 动态拼接元素样式问题 (1)
- sql迭代 (1)
- debug断点 (1)
- spring mvc 扫描注解(ioc (1)
- di) (1)
- jdk (2)
- cglib (1)
- mybatis 多层括号(超过三层)解析不了 (1)
- tiles (2)
- cacheManager缓存的切换 (1)
- jsp,xml中el表达式等占位符中的数据操作 (1)
- jpa 及spring data jpa开发 (1)
- compareTo equals toString (1)
- 子页面用父页面js (1)
- jsp相对路径自加问题 (1)
- bean之间的相互拷贝 (1)
- DECODE (2)
- 泛型 (2)
- NVL (1)
- jsp jstl函数 用标签声明页面变量,供jstl使用,时间格式问题 (1)
- form表单切换action值 (1)
- response.getWriter().write()功能优于springMvc的返回 (1)
- 快速复制构建项目 (1)
- activeMq (15)
- cache (2)
- sql获取序列号 (1)
- 上传 (2)
- tfs (1)
- jsp标签 (2)
- qita (1)
- 触发器 (1)
- Exception (1)
- mybais (1)
- 标签 (1)
- connect by (1)
- for ... in (1)
- map转化为list (1)
- 树形结构list构建(树形实体) (1)
- 左侧菜单的设计 (1)
- response.getWriter().write() (1)
- eclipse打断点之后断无效 (1)
- 直接访问/web-inf/下的页面 (1)
- clob (1)
- freemark (1)
- 框架的设计 (1)
- get方式处理乱码 (1)
- Request的getParameter和getAttribute方法的区别 (1)
- cas (9)
- 时间格式问题 (1)
- ResponseUtil.writeToResponse (1)
- 树形数据的反选 (1)
- spring data jpa (1)
- jsp异常提示 (1)
- jquery js (1)
- eclipse (1)
- 乱码 (1)
- Json OBJETC (1)
- PROCEDURE (1)
- pl/sql oracle (1)
- 设计 (1)
- el表达式 (3)
- iframe (1)
- map (1)
- jsp中调用Java (1)
- response.getWriter().write() ajax (1)
- mybatis xml 传入参数 (0)
- response与request在返回时作用区别 (1)
- spring cache (1)
- 模型驱动 (1)
- 关于mybatis传空值的处理 (1)
- 日志配置 (1)
- mapper (1)
- mapper mybatis (1)
- mapper mybatis-spring spring-data-jpa (1)
- js插件 (1)
- spring-data-jpa (2)
- 字节流转化过程 (1)
- 数据库 (11)
- jsp页面 (1)
- 局域网络访问问题 (1)
- plsql (2)
- response (1)
- plsq (1)
- Proxool (1)
- cas security (1)
- 系统配置化 (1)
- 集群 (4)
- springboot (24)
- 设计模式 (5)
- 通知 (1)
- 架构 (48)
- zookeeper (5)
- mvc框架 (1)
- 事物隔离策略 (1)
- jvm (8)
- 调研 (1)
- Java工具类 (1)
- 并发容器 (3)
- 多线程管理器 (4)
- 简历 (0)
- 查询 (1)
- 集群工具 (1)
- springMcv (1)
- ConcurrentHashMap (1)
- hashtable (1)
- cac (1)
- 树形结构 (1)
- 定时任务 (1)
- tortoiseGit (2)
- struts (11)
- shiro (31)
- log4j (3)
- struts2 (2)
- 编码 (1)
- request (3)
- Nginx (14)
- tomcat (1)
- idea (12)
- mvc (2)
- BeanUtils (2)
- image (1)
- mino (1)
- httpClient (1)
- volecity (1)
- swagger (3)
- 调错 (0)
- data (1)
- log (1)
- shell (1)
- 事物 (3)
- junit (1)
- RestTemplate (2)
- 线程 (0)
- Exception异常处理 (1)
- Exception异常注解 (1)
- lock (1)
- HashMap (1)
- 面试 (0)
- rabbitMq (1)
- rainCat (1)
- 其它 (1)
- activemq消息传送机制以及ACK机制详解 (0)
- mysql (7)
- fildder (1)
- UML (1)
- 校验 (1)
- 反射 (1)
- 切面 (1)
- springioc (1)
- urule2 (0)
- skywalking (1)
- urule (2)
- docker (2)
- 前端 (1)
- bootstrap (1)
- eureka (2)
- springcloud (16)
- jenkins (2)
- springsecurity (1)
- 数据结构 (4)
- gradle (1)
- mycat (3)
- nacos (1)
- canary (1)
- 线程池 (1)
- solr (1)
- resteasy (2)
- BI (1)
- dfs (2)
- elasticsearch (1)
- ruby (1)
- logstash (1)
- clickhouse (2)
- davinci (15)
- java (1)
- davinvi (1)
- fastdfs (1)
- vue (1)
- 《将博客搬至CSDN》 (1)
最新评论
-
nizhipeng123:
private SessionFactory sessio ...
用map接收参数的几个注意的问题
package com.ces.zwww.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author bogege
*
*/
public class HttpClient {
private String url;
private String reportMethod;
private boolean isOutPut = true;
private boolean isInput = true;
private boolean allowUserInteraction = true;
private BufferedReader reader;
public HttpClient() {
}
public HttpClient(String url, String reportMethod, boolean... otherParam) {
this.url = url;
this.reportMethod = reportMethod;
if (otherParam.length > 0) {
int len = otherParam.length;
for (int i = 0; i < len; i++) {
if (len == 1)
isOutPut = otherParam[0];
if (len == 2) {
isOutPut = otherParam[0];
isInput = otherParam[1];
}
if (len == 3) {
isOutPut = otherParam[0];
isInput = otherParam[1];
allowUserInteraction = otherParam[2];
}
}
}
}
/**
* 获取返回的字符串
* @return
* @throws Exception
*/
public String getResponseData() throws Exception {
try {
String strMessage = null;
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpConnection = getHttpCon();
if(httpConnection == null ){
return strMessage;
}
InputStream inputStream = httpConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((strMessage = reader.readLine()) != null) {
buffer.append(strMessage);
}
closeHttpCon(httpConnection);
return buffer.toString();
} catch (Exception e) {
throw e;
} finally{
if(reader != null){
reader.close();
}
}
}
/**
* 获取连接
* @param url
* 请求的路径
* @param reportMethod
* 请求的方法 post,get等
* @return
*/
private HttpURLConnection getHttpCon() throws Exception {
try {
URL u = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) u
.openConnection();
httpConnection.setRequestMethod(reportMethod);
httpConnection.setDoOutput(isOutPut);
httpConnection.setDoInput(isInput);
httpConnection.setAllowUserInteraction(allowUserInteraction);
int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
throw new Exception("responseCode:"+responseCode);
}
return httpConnection;
} catch (MalformedURLException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
private void closeHttpCon(HttpURLConnection httpConnection) {
httpConnection.disconnect();
}
public boolean isOutPut() {
return isOutPut;
}
public void setOutPut(boolean isOutPut) {
this.isOutPut = isOutPut;
}
public boolean isInput() {
return isInput;
}
public void setInput(boolean isInput) {
this.isInput = isInput;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getReportMethod() {
return reportMethod;
}
public void setReportMethod(String reportMethod) {
this.reportMethod = reportMethod;
}
public void setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
}
}
package com.ces.zwww.utils;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author bogege
*/
public class HttpServer extends Thread{
private File documentRootDirectory;
private String indexFileName = "index.html";
private ServerSocket server;
private int numThreads = 50;
public HttpServer(File documentRootDirectory, int port, String indexFileName)
throws IOException {
if (!documentRootDirectory.isDirectory()) {
throw new IOException(documentRootDirectory + " does not exist as a directory ");
}
this.documentRootDirectory = documentRootDirectory;
this.indexFileName = indexFileName;
this.server = new ServerSocket(port);
}
private HttpServer(File documentRootDirectory, int port) throws IOException {
this(documentRootDirectory, port, "index.html");
}
public void run() {
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(new RequestProcessor(documentRootDirectory, indexFileName));
t.start();
}
System.out.println("Accepting connection on port " + server.getLocalPort());
System.out.println("Document Root: " + documentRootDirectory);
while (true) {
try {
Socket request = server.accept();
RequestProcessor.processRequest(request);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File docroot;
try {
docroot = new File("D:\\temp");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: java HttpServer docroot port indexfile");
return;
}
int port;
try {
port = Integer.parseInt(args[1]);
if (port < 0 || port > 65535) {
port = 80;
}
} catch (Exception e) {
port = 80;
}
try {
HttpServer webserver = new HttpServer(docroot, port);
webserver.start();
} catch (IOException e) {
System.out.println("Server could not start because of an " + e.getClass());
System.out.println(e);
}
}
}
package com.ces.zwww.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import com.ces.zwww.entity.Tbresource;
/**
*
* @author bogege
*
*/
public class RequestProcessor implements Runnable{
private static List pool=new LinkedList();
private File documentRootDirectory;
private String indexFileName="index.html";
public RequestProcessor(File documentRootDirectory,String indexFileName) {
if (documentRootDirectory.isFile()) {
throw new IllegalArgumentException();
}
this.documentRootDirectory=documentRootDirectory;
try {
this.documentRootDirectory=documentRootDirectory.getCanonicalFile();
} catch (IOException e) {
}
if (indexFileName!=null) {
this.indexFileName=indexFileName;
}
}
public static void processRequest(Socket request) {
synchronized (pool) {
pool.add(pool.size(),request);
pool.notifyAll();
}
}
@Override
public void run() {
//安全性检测
String root=documentRootDirectory.getPath();
while (true) {
Socket connection;
synchronized (pool) {
while (pool.isEmpty()) {
try {
pool.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
connection=(Socket)pool.remove(0);
}
try {
String fileName;
String contentType;
OutputStream raw = new BufferedOutputStream(connection.getOutputStream());
Writer out = new OutputStreamWriter(raw, "UTF-8");
Reader in=new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
StringBuffer request=new StringBuffer(80);
//刚进来的时候肯定是空,一直读,读到最后
while (true) {
int c=in.read();
if (c=='\t'||c=='\n'||c==-1) {
break;
}
request.append((char)c);
}
String get=request.toString();
//记录日志
System.out.println(get);
StringTokenizer st=new StringTokenizer(get);
String method=st.nextToken();
String version="";
///输入中传要的方法,拿出来匹配,再输出
if ("GET".equals(method)) {
Tbresource tb = new Tbresource();
tb.setId("test123");
tb.setIp("1.1.1.1");
//tb.setIsfavorite(1);
//tb.setIsnetdevice(0);
// tb.setResourceid("1dsf-23");
// tb.setResourcename("資源1");
/// tb.setResourcestatus("1");
// tb.setResourcetype("213");
out.write("HTTP/1.0 200 OK\r\n");
out.write("Date: 2014-01-01\r\n");
out.write("Server: JHTTP 1.0\r\n");
out.write("Content-Type: text/html\r\n\r\n");
out.write(JsonUtil.objectToJsonStr(tb));
out.flush();
}
} catch (IOException e) {
}finally{
try {
connection.close();
} catch (IOException e2) {
}
}
}
}
// @Override
// public void run() {
// //安全性检测
// String root=documentRootDirectory.getPath();
//
// while (true) {
// Socket connection;
// synchronized (pool) {
// while (pool.isEmpty()) {
// try {
// pool.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// connection=(Socket)pool.remove(0);
// }
// try {
// String fileName;
// String contentType;
// OutputStream raw = new BufferedOutputStream(connection.getOutputStream());
// Writer out = new OutputStreamWriter(raw, "UTF-8");
//
// Reader in=new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
// StringBuffer request=new StringBuffer(80);
// while (true) {
// int c=in.read();
// if (c=='\t'||c=='\n'||c==-1) {
// break;
// }
// request.append((char)c);
// }
//
// String get=request.toString();
//
// //记录日志
// System.out.println(get);
//
// StringTokenizer st=new StringTokenizer(get);
// String method=st.nextToken();
// String version="";
// if ("GET".equals(method)) {
// fileName=st.nextToken();
// if (fileName.endsWith("/")) {
// fileName+=indexFileName;
// }
// contentType=guessContentTypeFromName(fileName);
// if (st.hasMoreTokens()) {
// version=st.nextToken();
// }
//
// File theFile=new File(documentRootDirectory,fileName.substring(1,fileName.length()));
// if (theFile.canRead()&&theFile.getCanonicalPath().startsWith(root)) {
// DataInputStream fis=new DataInputStream(new BufferedInputStream(new FileInputStream(theFile)));
// byte[] theData=new byte[(int)theFile.length()];
// fis.readFully(theData);
// fis.close();
// if (version.startsWith("HTTP")) {
// out.write("HTTP/1.0 200 OK\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-length: "+theData.length+"\r\n");
// out.write("Content-Type: "+contentType+"\r\n\r\n");
// out.flush();
// }
// raw.write(theData);
// raw.flush();
// }else {
// if (version.startsWith("HTTP ")) {
// out.write("HTTP/1.0 404 File Not Found\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-Type: text/html\r\n\r\n");
// out.flush();
// }
// out.write("<HTML>\r\n");
// out.write("<HEAD><TITLE>File Not Found</TITLE></HRAD>\r\n");
// out.write("<BODY>\r\n");
// out.write("<H1>HTTP Error 404: File Not Found</H1>");
// out.write("</BODY></HTML>\r\n");
// }
// }else {//方法不等于"GET"
// if (version.startsWith("HTTP ")) {
// out.write("HTTP/1.0 501 Not Implemented\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-Type: text/html\r\n\r\n");
// out.flush();
// }
// out.write("<HTML>\r\n");
// out.write("<HEAD><TITLE>Not Implemented</TITLE></HRAD>\r\n");
// out.write("<BODY>\r\n");
// out.write("<H1>HTTP Error 501: Not Implemented</H1>");
// out.write("</BODY></HTML>\r\n");
// }
//
// } catch (IOException e) {
// }finally{
// try {
// connection.close();
// } catch (IOException e2) {
// }
//
// }
// }
// }
public static String guessContentTypeFromName(String name) {
if (name.endsWith(".html")||name.endsWith(".htm")) {
return "text/html";
}else if (name.endsWith(".txt")||name.endsWith(".java")) {
return "text/plain";
}else if (name.endsWith(".gif")) {
return "image/gif";
}else if (name.endsWith(".class")) {
return "application/octet-stream";
}else if (name.endsWith(".jpg")||name.endsWith(".jpeg")) {
return "image/jpeg";
}else {
return "text/plain";
}
}
}
另外的调用示例
url="http://218.242.121.41:8890/itsm/rest/api/itsm/tickets?moduleId=incident&createEndTime=2014-7-30";
String ui = "http://localhost:8890/itsm/rest/api/itsm/tickets?moduleId=incident&createStartTime=2014-7-29&createEndTime=2014-7-30";
URL url1 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json;charset=utf-8");
System.out.println("response code:" + conn.getResponseCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String result;
System.out.println("content:");
result = reader.readLine();
System.out.println(result);
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* @author bogege
*
*/
public class HttpClient {
private String url;
private String reportMethod;
private boolean isOutPut = true;
private boolean isInput = true;
private boolean allowUserInteraction = true;
private BufferedReader reader;
public HttpClient() {
}
public HttpClient(String url, String reportMethod, boolean... otherParam) {
this.url = url;
this.reportMethod = reportMethod;
if (otherParam.length > 0) {
int len = otherParam.length;
for (int i = 0; i < len; i++) {
if (len == 1)
isOutPut = otherParam[0];
if (len == 2) {
isOutPut = otherParam[0];
isInput = otherParam[1];
}
if (len == 3) {
isOutPut = otherParam[0];
isInput = otherParam[1];
allowUserInteraction = otherParam[2];
}
}
}
}
/**
* 获取返回的字符串
* @return
* @throws Exception
*/
public String getResponseData() throws Exception {
try {
String strMessage = null;
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpConnection = getHttpCon();
if(httpConnection == null ){
return strMessage;
}
InputStream inputStream = httpConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((strMessage = reader.readLine()) != null) {
buffer.append(strMessage);
}
closeHttpCon(httpConnection);
return buffer.toString();
} catch (Exception e) {
throw e;
} finally{
if(reader != null){
reader.close();
}
}
}
/**
* 获取连接
* @param url
* 请求的路径
* @param reportMethod
* 请求的方法 post,get等
* @return
*/
private HttpURLConnection getHttpCon() throws Exception {
try {
URL u = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) u
.openConnection();
httpConnection.setRequestMethod(reportMethod);
httpConnection.setDoOutput(isOutPut);
httpConnection.setDoInput(isInput);
httpConnection.setAllowUserInteraction(allowUserInteraction);
int responseCode = httpConnection.getResponseCode();
if (responseCode >= 400) {
throw new Exception("responseCode:"+responseCode);
}
return httpConnection;
} catch (MalformedURLException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
private void closeHttpCon(HttpURLConnection httpConnection) {
httpConnection.disconnect();
}
public boolean isOutPut() {
return isOutPut;
}
public void setOutPut(boolean isOutPut) {
this.isOutPut = isOutPut;
}
public boolean isInput() {
return isInput;
}
public void setInput(boolean isInput) {
this.isInput = isInput;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getReportMethod() {
return reportMethod;
}
public void setReportMethod(String reportMethod) {
this.reportMethod = reportMethod;
}
public void setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
}
}
package com.ces.zwww.utils;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author bogege
*/
public class HttpServer extends Thread{
private File documentRootDirectory;
private String indexFileName = "index.html";
private ServerSocket server;
private int numThreads = 50;
public HttpServer(File documentRootDirectory, int port, String indexFileName)
throws IOException {
if (!documentRootDirectory.isDirectory()) {
throw new IOException(documentRootDirectory + " does not exist as a directory ");
}
this.documentRootDirectory = documentRootDirectory;
this.indexFileName = indexFileName;
this.server = new ServerSocket(port);
}
private HttpServer(File documentRootDirectory, int port) throws IOException {
this(documentRootDirectory, port, "index.html");
}
public void run() {
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(new RequestProcessor(documentRootDirectory, indexFileName));
t.start();
}
System.out.println("Accepting connection on port " + server.getLocalPort());
System.out.println("Document Root: " + documentRootDirectory);
while (true) {
try {
Socket request = server.accept();
RequestProcessor.processRequest(request);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File docroot;
try {
docroot = new File("D:\\temp");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: java HttpServer docroot port indexfile");
return;
}
int port;
try {
port = Integer.parseInt(args[1]);
if (port < 0 || port > 65535) {
port = 80;
}
} catch (Exception e) {
port = 80;
}
try {
HttpServer webserver = new HttpServer(docroot, port);
webserver.start();
} catch (IOException e) {
System.out.println("Server could not start because of an " + e.getClass());
System.out.println(e);
}
}
}
package com.ces.zwww.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import com.ces.zwww.entity.Tbresource;
/**
*
* @author bogege
*
*/
public class RequestProcessor implements Runnable{
private static List pool=new LinkedList();
private File documentRootDirectory;
private String indexFileName="index.html";
public RequestProcessor(File documentRootDirectory,String indexFileName) {
if (documentRootDirectory.isFile()) {
throw new IllegalArgumentException();
}
this.documentRootDirectory=documentRootDirectory;
try {
this.documentRootDirectory=documentRootDirectory.getCanonicalFile();
} catch (IOException e) {
}
if (indexFileName!=null) {
this.indexFileName=indexFileName;
}
}
public static void processRequest(Socket request) {
synchronized (pool) {
pool.add(pool.size(),request);
pool.notifyAll();
}
}
@Override
public void run() {
//安全性检测
String root=documentRootDirectory.getPath();
while (true) {
Socket connection;
synchronized (pool) {
while (pool.isEmpty()) {
try {
pool.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
connection=(Socket)pool.remove(0);
}
try {
String fileName;
String contentType;
OutputStream raw = new BufferedOutputStream(connection.getOutputStream());
Writer out = new OutputStreamWriter(raw, "UTF-8");
Reader in=new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
StringBuffer request=new StringBuffer(80);
//刚进来的时候肯定是空,一直读,读到最后
while (true) {
int c=in.read();
if (c=='\t'||c=='\n'||c==-1) {
break;
}
request.append((char)c);
}
String get=request.toString();
//记录日志
System.out.println(get);
StringTokenizer st=new StringTokenizer(get);
String method=st.nextToken();
String version="";
///输入中传要的方法,拿出来匹配,再输出
if ("GET".equals(method)) {
Tbresource tb = new Tbresource();
tb.setId("test123");
tb.setIp("1.1.1.1");
//tb.setIsfavorite(1);
//tb.setIsnetdevice(0);
// tb.setResourceid("1dsf-23");
// tb.setResourcename("資源1");
/// tb.setResourcestatus("1");
// tb.setResourcetype("213");
out.write("HTTP/1.0 200 OK\r\n");
out.write("Date: 2014-01-01\r\n");
out.write("Server: JHTTP 1.0\r\n");
out.write("Content-Type: text/html\r\n\r\n");
out.write(JsonUtil.objectToJsonStr(tb));
out.flush();
}
} catch (IOException e) {
}finally{
try {
connection.close();
} catch (IOException e2) {
}
}
}
}
// @Override
// public void run() {
// //安全性检测
// String root=documentRootDirectory.getPath();
//
// while (true) {
// Socket connection;
// synchronized (pool) {
// while (pool.isEmpty()) {
// try {
// pool.wait();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// connection=(Socket)pool.remove(0);
// }
// try {
// String fileName;
// String contentType;
// OutputStream raw = new BufferedOutputStream(connection.getOutputStream());
// Writer out = new OutputStreamWriter(raw, "UTF-8");
//
// Reader in=new InputStreamReader(new BufferedInputStream(connection.getInputStream()));
// StringBuffer request=new StringBuffer(80);
// while (true) {
// int c=in.read();
// if (c=='\t'||c=='\n'||c==-1) {
// break;
// }
// request.append((char)c);
// }
//
// String get=request.toString();
//
// //记录日志
// System.out.println(get);
//
// StringTokenizer st=new StringTokenizer(get);
// String method=st.nextToken();
// String version="";
// if ("GET".equals(method)) {
// fileName=st.nextToken();
// if (fileName.endsWith("/")) {
// fileName+=indexFileName;
// }
// contentType=guessContentTypeFromName(fileName);
// if (st.hasMoreTokens()) {
// version=st.nextToken();
// }
//
// File theFile=new File(documentRootDirectory,fileName.substring(1,fileName.length()));
// if (theFile.canRead()&&theFile.getCanonicalPath().startsWith(root)) {
// DataInputStream fis=new DataInputStream(new BufferedInputStream(new FileInputStream(theFile)));
// byte[] theData=new byte[(int)theFile.length()];
// fis.readFully(theData);
// fis.close();
// if (version.startsWith("HTTP")) {
// out.write("HTTP/1.0 200 OK\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-length: "+theData.length+"\r\n");
// out.write("Content-Type: "+contentType+"\r\n\r\n");
// out.flush();
// }
// raw.write(theData);
// raw.flush();
// }else {
// if (version.startsWith("HTTP ")) {
// out.write("HTTP/1.0 404 File Not Found\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-Type: text/html\r\n\r\n");
// out.flush();
// }
// out.write("<HTML>\r\n");
// out.write("<HEAD><TITLE>File Not Found</TITLE></HRAD>\r\n");
// out.write("<BODY>\r\n");
// out.write("<H1>HTTP Error 404: File Not Found</H1>");
// out.write("</BODY></HTML>\r\n");
// }
// }else {//方法不等于"GET"
// if (version.startsWith("HTTP ")) {
// out.write("HTTP/1.0 501 Not Implemented\r\n");
// Date now=new Date();
// out.write("Date: "+now+"\r\n");
// out.write("Server: JHTTP 1.0\r\n");
// out.write("Content-Type: text/html\r\n\r\n");
// out.flush();
// }
// out.write("<HTML>\r\n");
// out.write("<HEAD><TITLE>Not Implemented</TITLE></HRAD>\r\n");
// out.write("<BODY>\r\n");
// out.write("<H1>HTTP Error 501: Not Implemented</H1>");
// out.write("</BODY></HTML>\r\n");
// }
//
// } catch (IOException e) {
// }finally{
// try {
// connection.close();
// } catch (IOException e2) {
// }
//
// }
// }
// }
public static String guessContentTypeFromName(String name) {
if (name.endsWith(".html")||name.endsWith(".htm")) {
return "text/html";
}else if (name.endsWith(".txt")||name.endsWith(".java")) {
return "text/plain";
}else if (name.endsWith(".gif")) {
return "image/gif";
}else if (name.endsWith(".class")) {
return "application/octet-stream";
}else if (name.endsWith(".jpg")||name.endsWith(".jpeg")) {
return "image/jpeg";
}else {
return "text/plain";
}
}
}
另外的调用示例
url="http://218.242.121.41:8890/itsm/rest/api/itsm/tickets?moduleId=incident&createEndTime=2014-7-30";
String ui = "http://localhost:8890/itsm/rest/api/itsm/tickets?moduleId=incident&createStartTime=2014-7-29&createEndTime=2014-7-30";
URL url1 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json;charset=utf-8");
System.out.println("response code:" + conn.getResponseCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String result;
System.out.println("content:");
result = reader.readLine();
System.out.println(result);
发表评论
文章已被作者锁定,不允许评论。
相关推荐
在Java编程中,HTTP POST和GET是两种基本的HTTP请求方法,用于客户端向服务器发送数据。为了在Java中实现这些功能,我们需要引入特定的库,这些库通常被打包成JAR(Java Archive)文件。本篇文章将详细讲解HTTP POST...
import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; ...
《深入理解HTTPCore与HTTPMIME:构建高效网络通信》 HTTPCore与HTTPMIME是Apache HttpClient库中的核心组件,这两个jar包在Java开发者中有着广泛的应用,尤其在处理HTTP请求和响应时不可或缺。本篇将详细介绍这两个...
在这个特定的场景中,我们关注的是如何利用VC通过HttpGet和HttpPost方法与WebService进行交互,并处理返回的Json数据。 HttpGet和HttpPost是HTTP协议中的两种主要请求方法。HttpGet是一种无状态、幂等的请求方法,...
1. **HTTP协议支持**:httpcore提供了对HTTP/1.0和HTTP/1.1协议的支持,包括GET、POST等各种HTTP方法,以及头部处理、状态码解析等。 2. **连接管理**:它包含连接池(Connection Pooling)功能,可以复用HTTP连接...
import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; ...
org.apache.http jar包 import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org....
**HTTP协议详解** HTTP(Hypertext Transfer Protocol)超文本传输协议是互联网上应用最广泛的一种网络协议。它是用于从万维网服务器传输超文本到本地浏览器的传输协议,是Web应用的基础。HTTP协议定义了客户端...
在IT行业中,网络通信是软件开发的一个重要领域,而HTTP(超文本传输协议)作为互联网上应用最广泛的一种网络协议,被广泛应用于网页浏览、数据传输等场景。本篇文章将详细探讨如何使用C++来实现HTTP的POST和GET请求...
构建一个简单的HTTP服务器的C#程序实例。实现响应GET、POST请求。在服务端创建一个tcp通信来负责监听客户端连接。每次客户端发出请求后,我们根据请问报文来判断客户端的请求类型,然后根据不同的请求类型进行相应的...
标题 "VB做的HTTP简单服务器源码" 描述的是一个使用Visual Basic(VB)编程语言编写的简易HTTP服务器的源代码。这个服务器能够响应HTTP请求,为客户端提供服务,可能是为了教学目的或者作为小型项目的起点。VB是一种...
在IT行业中,网络通信是至关重要的一个领域,而HTTP(超文本传输协议)作为网络通信的基础,被广泛应用于Web应用程序的开发。在这个场景下,我们关注的是使用VC++(Microsoft Visual C++)来实现HTTP客户端和服务器...
### 图解HTTP:全面解析HTTP协议 #### 一、引言 HTTP协议作为互联网的核心组成部分,对于每一个从事Web开发或维护的技术人员来说都是必须掌握的基础知识。《图解HTTP》一书通过丰富的图表和深入浅出的文字解释,...
import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import...
在IT领域,网络编程是不可或缺的一部分,特别是在操作系统如Linux中,开发者经常需要通过HTTP协议进行数据交换。本篇将深入探讨使用C++在Linux环境下实现HTTP请求,包括GET和POST方法。 HTTP(超文本传输协议)是...
import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org....
在本示例中,我们将关注的是"C# HTTP接口设计及调用demo",这通常涉及到如何创建一个HTTP服务端接口,以及如何使用C#客户端进行调用。HTTP接口在分布式系统中扮演着重要角色,它允许不同组件之间通过HTTP协议交换...
在本项目中,"网络编程HttpServer c++实现" 是一个使用C++语言编写的HTTP服务器,它允许用户创建和管理基于HTTP协议的服务。HTTP服务器是互联网上的关键组件,它们接收HTTP请求并返回HTTP响应,使得网页和其他资源...
在VC++编程环境中,HTTP(超文本传输协议)Get和Post请求是常见的网络通信方法,用于从或向服务器发送数据。这两个方法是Web应用程序与服务器交互的基础,理解它们的工作原理和如何在VC++中实现至关重要。 **HTTP ...