parent
d37fd9b9f4
commit
d1cac9240e
42 changed files with 859 additions and 28 deletions
@ -0,0 +1,11 @@ |
||||
package org.panda.beans.adapter; |
||||
|
||||
/** |
||||
* @Author: qi |
||||
* @Description: |
||||
* @Date: Create in 9:29 AM 2019/2/23 |
||||
*/ |
||||
public interface BeanAdapter { |
||||
|
||||
|
||||
} |
@ -0,0 +1,96 @@ |
||||
package org.panda.beans.factory; |
||||
|
||||
/** |
||||
* @Author: qi |
||||
* @Description: |
||||
* @Date: Create in 9:06 AM 2019/1/29 |
||||
* 实例工厂 |
||||
*/ |
||||
public interface BeanFactory { |
||||
|
||||
/** |
||||
* 实例 -> 工厂转义符 |
||||
*/ |
||||
String FACTORY_BEAN_PREFIX = "&"; |
||||
|
||||
/** |
||||
* 通过类名查找实例 |
||||
* @param name |
||||
* @return |
||||
*/ |
||||
Object getBean(String name); |
||||
|
||||
/** |
||||
* 通过类名查找实例并确定类型 |
||||
* @param name |
||||
* @param requiredType |
||||
* @param <T> |
||||
* @return |
||||
*/ |
||||
<T> T getBean(String name,Class<T> requiredType); |
||||
|
||||
/** |
||||
* 通过类名查找实例 |
||||
* @param name |
||||
* @param args |
||||
* @return |
||||
*/ |
||||
Object getBean(String name,Object... args); |
||||
|
||||
/** |
||||
* 通过class查找实例 |
||||
* @param requiredType |
||||
* @param <T> |
||||
* @return |
||||
*/ |
||||
<T> T getBean(Class<T> requiredType); |
||||
|
||||
|
||||
/** |
||||
* 通过实例名,返回实例class |
||||
* @param name |
||||
* @return |
||||
*/ |
||||
Class<?> getType(String name); |
||||
|
||||
/** |
||||
* 通过实例名返回该实例的别名数组 |
||||
* @param name |
||||
* @return |
||||
*/ |
||||
String[] getAliases(String name); |
||||
|
||||
/** |
||||
* 通过实例名判断实例是否存在 |
||||
* @param name |
||||
* @return 存在:true 不存在:false |
||||
*/ |
||||
boolean containsBean(String name); |
||||
|
||||
/** |
||||
* singletion和protpType区别 |
||||
* 1.singletion在容器中只有一个实例,protoType则是每次getBean的时候创建一个实例 |
||||
*/ |
||||
|
||||
/** |
||||
* 通过实例名判断实例是否是单例 |
||||
* @param name |
||||
* @return 是:true 否:false |
||||
*/ |
||||
boolean isSingletion(String name); |
||||
|
||||
/** |
||||
* 通过实例名判断实例是否原型 |
||||
* @param name |
||||
* @return 是:true 否:false |
||||
*/ |
||||
boolean isProtoType(String name); |
||||
|
||||
/** |
||||
* 检查实例是否与class类型是否匹配 |
||||
* @param name |
||||
* @param typeToMatch |
||||
* @return 是:true 否:false |
||||
*/ |
||||
boolean isTypeMatch(String name,Class<?> typeToMatch); |
||||
} |
@ -0,0 +1,51 @@ |
||||
package org.panda.beans.entity; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* @Author: qi |
||||
* @Description: |
||||
* @Date: Create in 10:25 AM 2019/2/28 |
||||
*/ |
||||
public class SysUser { |
||||
|
||||
private Long id; |
||||
|
||||
private String name; |
||||
|
||||
private Date createTime; |
||||
|
||||
private Date updateTime; |
||||
|
||||
public Long getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(Long id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public Date getCreateTime() { |
||||
return createTime; |
||||
} |
||||
|
||||
public void setCreateTime(Date createTime) { |
||||
this.createTime = createTime; |
||||
} |
||||
|
||||
public Date getUpdateTime() { |
||||
return updateTime; |
||||
} |
||||
|
||||
public void setUpdateTime(Date updateTime) { |
||||
this.updateTime = updateTime; |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
package org.panda.beans.util; |
||||
|
||||
import org.junit.Test; |
||||
import org.panda.beans.entity.SysUser; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.Date; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @Author: qi |
||||
* @Description: |
||||
* @Date: Create in 10:25 AM 2019/2/28 |
||||
*/ |
||||
public class BeanUtilTest { |
||||
|
||||
@Test |
||||
public void methodTest(){ |
||||
SysUser sysUser = new SysUser(); |
||||
sysUser.setId(1L); |
||||
sysUser.setName("liangqi"); |
||||
sysUser.setCreateTime(new Date()); |
||||
sysUser.setUpdateTime(new Date()); |
||||
|
||||
Class clazz = sysUser.getClass(); |
||||
Method[] methods = clazz.getMethods(); |
||||
for (Method method:methods){ |
||||
System.out.println(method.getName()); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,175 @@ |
||||
package org.panda.code.uitl.http; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import org.apache.http.Header; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.client.ClientProtocolException; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpGet; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import org.apache.http.entity.ContentType; |
||||
import org.apache.http.entity.mime.HttpMultipartMode; |
||||
import org.apache.http.entity.mime.MultipartEntityBuilder; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import java.io.*; |
||||
import java.nio.charset.Charset; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.UUID; |
||||
|
||||
public class HttpsClient { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(HttpsClient.class); |
||||
|
||||
/** |
||||
* http 默认字符编码 |
||||
*/ |
||||
public static final String DETAFILE_CHARSET = "UTF-8"; |
||||
|
||||
public static String post(String url,Map<String, Object> params) throws IOException{ |
||||
String boundary = getBoundaryStr(); |
||||
CloseableHttpResponse reponse =null; |
||||
CloseableHttpClient client = null; |
||||
String resultStr = ""; |
||||
try{ |
||||
// 获得utf-8编码的mbuilder
|
||||
MultipartEntityBuilder mBuilder = get_COMPATIBLE_Builder(DETAFILE_CHARSET,boundary); |
||||
/** |
||||
* 原生的微信使用的url是https://api.weixin.qq.com/cgi-bin/media/upload?
|
||||
* access_token=##ACCESS_TOKEN##&type=##TYPE## |
||||
* 一般都会使用这个把参数直接携带在url中。我个人不喜欢这样,因为既然使用了httpclient,完全可以把参数 |
||||
* 设置在我们的body体中。所以我们使用的url是这样的 |
||||
* https://api.weixin.qq.com/cgi-bin/media/upload 然后通过在body体中设置参数来设置
|
||||
* access_token和type这两个字段 |
||||
* |
||||
* */ |
||||
params.forEach((key,value)->{ |
||||
if (value instanceof String){ |
||||
mBuilder.addTextBody(key, (String) value); |
||||
} |
||||
if (value instanceof File){ |
||||
File file = (File)value; |
||||
mBuilder.addBinaryBody(key,file, |
||||
ContentType.APPLICATION_OCTET_STREAM,file |
||||
.getName()); |
||||
} |
||||
}); |
||||
|
||||
// 建造我们的http多媒体对象
|
||||
HttpEntity he = mBuilder.build(); |
||||
// 建立一个sslcontext,这里我们信任任何的证书。
|
||||
SSLContext context = getTrustAllSSLContext(); |
||||
// 建立socket工厂
|
||||
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory( |
||||
context); |
||||
// 建立连接器
|
||||
client = HttpClients.custom() |
||||
.setSSLSocketFactory(factory).build(); |
||||
// 得到一个post请求的实体
|
||||
HttpPost post = getMultipartPost(url, boundary); |
||||
// 给请求添加参数
|
||||
post.setEntity(he); |
||||
// 执行请求并获得结果
|
||||
reponse = client.execute(post); |
||||
// 获得返回的内容
|
||||
HttpEntity entity = reponse.getEntity(); |
||||
// 输出
|
||||
System.out.println(JSONObject.toJSONString(entity)); |
||||
|
||||
resultStr = JSONObject.toJSONString(entity); |
||||
|
||||
Reader reader = new BufferedReader(new InputStreamReader(entity.getContent())); |
||||
String line = null; |
||||
StringBuffer buffer = new StringBuffer(); |
||||
while (reader.ready()){ |
||||
buffer.append((char)reader.read()); |
||||
} |
||||
System.out.println(buffer.toString()); |
||||
} catch (ClientProtocolException e) { |
||||
e.printStackTrace(); |
||||
} catch (IOException e) { |
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace(); |
||||
} catch (Exception e) { |
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace(); |
||||
} finally{ |
||||
// 关闭返回的reponse
|
||||
if(reponse!=null){ |
||||
reponse.close(); |
||||
} |
||||
if(client!=null){ |
||||
// 关闭client
|
||||
client.close(); |
||||
} |
||||
} |
||||
return resultStr; |
||||
} |
||||
|
||||
private static String getBoundaryStr() { |
||||
String boundary = UUID.randomUUID().toString().replace("-", ""); |
||||
return boundary; |
||||
} |
||||
|
||||
|
||||
private static MultipartEntityBuilder get_COMPATIBLE_Builder(String charSet,String boundarStr) { |
||||
MultipartEntityBuilder result = MultipartEntityBuilder.create(); |
||||
result.setBoundary(boundarStr) |
||||
.setCharset(Charset.forName(charSet)) |
||||
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
||||
return result; |
||||
} |
||||
|
||||
|
||||
private static HttpPost getMultipartPost(String url,String boundarStr) { |
||||
/* 这里设置一些post的头部信息,具体求百度吧 */ |
||||
HttpPost post = new HttpPost(url); |
||||
post.addHeader("Connection", "keep-alive"); |
||||
post.addHeader("Accept", "*/*"); |
||||
post.addHeader("Content-Type", "multipart/form-data;boundary=" |
||||
+ boundarStr); |
||||
post.addHeader("User-Agent", |
||||
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); |
||||
return post; |
||||
} |
||||
|
||||
private static HttpGet getHttpGet(String url){ |
||||
HttpGet get = new HttpGet(url); |
||||
|
||||
return get; |
||||
} |
||||
|
||||
private static SSLContext getTrustAllSSLContext() throws Exception { |
||||
// SSLContext context = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
|
||||
//
|
||||
// @Override
|
||||
// public boolean isTrusted(X509Certificate[] arg0, String arg1)
|
||||
// throws java.security.cert.CertificateException {
|
||||
// // 这一句就是信任任何的证书,当然你也可以去验证服务器的真实性
|
||||
// return true;
|
||||
// }
|
||||
// }).build();
|
||||
SSLContext context = SSLContext.getDefault(); |
||||
return context; |
||||
} |
||||
|
||||
public static void main(String[] args) throws IOException { |
||||
File file =new File("/Users/qi_liang/Downloads/timg.jpeg"); |
||||
String url = "https://api.weixin.qq.com/cgi-bin/media/upload"; |
||||
Map<String, Object> params = new HashMap<String, Object>(); |
||||
params.put("access_token", "10_0oKKCsF9JL30JeGZDyfNWDhiaRvivAL9vs9ihvaledzBuPVM6IMvwcFUc58AFwlETRdvEBAFXOYbF2GoKmbtMmopCs78m9UE8whgrYU4kJww4mSooMcq8el-qnU4JdjPGKruZLjdNS246hXyWOHbAAAHZS"); |
||||
params.put("type", "image"); |
||||
if(file.exists()){ |
||||
params.put("media", file); |
||||
} |
||||
String resultStr = post(url,params); |
||||
System.out.println(resultStr); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,107 @@ |
||||
package org.panda.code.uitl.os; |
||||
|
||||
import org.panda.code.uitl.os.mode.JvmInfo; |
||||
|
||||
import java.text.DecimalFormat; |
||||
import java.util.Properties; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author qi |
||||
* Created by qi_liang on 2018/6/9. |
||||
*/ |
||||
public class JvmUtil { |
||||
|
||||
static Properties properties = System.getProperties(); |
||||
static Runtime runtime = Runtime.getRuntime(); |
||||
|
||||
/** |
||||
* 获取java版本号 |
||||
* @return |
||||
*/ |
||||
public static String getVersion(){ |
||||
|
||||
return properties.getProperty("java.version"); |
||||
} |
||||
|
||||
/** |
||||
* 获取Java 环境供应商名称 |
||||
*/ |
||||
public static String getVendor(){ |
||||
|
||||
return properties.getProperty("java.vendor"); |
||||
} |
||||
|
||||
/** |
||||
* 获取java 环境供应商url |
||||
*/ |
||||
public static String getVendorUrl(){ |
||||
return properties.getProperty("java.vendor.url"); |
||||
} |
||||
|
||||
/** |
||||
* 获取java安装路径 |
||||
* @param |
||||
*/ |
||||
public static String getHome(){ |
||||
|
||||
return properties.getProperty("java.home"); |
||||
} |
||||
|
||||
/** |
||||
* 获取虚拟机可以使用的总内存 |
||||
*/ |
||||
public static Long getTotalMemory(){ |
||||
|
||||
return runtime.totalMemory(); |
||||
} |
||||
/** |
||||
* 获取虚拟机可以使用的数据器个数 |
||||
*/ |
||||
private static Integer getCupNum(){ |
||||
return runtime.availableProcessors(); |
||||
} |
||||
/** |
||||
* 返回jvm 信息 |
||||
*/ |
||||
|
||||
public static JvmInfo getInfo(){ |
||||
JvmInfo jvmInfo = new JvmInfo(); |
||||
jvmInfo.setVersion(getVersion()); |
||||
jvmInfo.setCupNum(getCupNum()); |
||||
jvmInfo.setTotalMemory(getTotalMemory()); |
||||
return jvmInfo; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param args |
||||
*/ |
||||
|
||||
/** |
||||
* Starts the program |
||||
* |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
new JvmUtil().displayAvailableMemory(); |
||||
} |
||||
|
||||
/** |
||||
* 显示JVM总内存,JVM最大内存和总空闲内存 |
||||
*/ |
||||
public void displayAvailableMemory() { |
||||
DecimalFormat df = new DecimalFormat("0.00") ; |
||||
|
||||
//显示JVM总内存
|
||||
long totalMem = Runtime.getRuntime().totalMemory(); |
||||
System.out.println(df.format(totalMem/1024/1024) + " MB"); |
||||
//显示JVM尝试使用的最大内存
|
||||
long maxMem = Runtime.getRuntime().maxMemory(); |
||||
System.out.println(df.format(maxMem ) + " MB"); |
||||
//空闲内存
|
||||
long freeMem = Runtime.getRuntime().freeMemory(); |
||||
System.out.println(df.format(freeMem ) + " MB"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@ |
||||
package org.panda.code.uitl.os; |
||||
|
||||
import org.panda.code.uitl.os.mode.OsInfo; |
||||
|
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* @author qi |
||||
* 获取计算机操作系统信息 |
||||
* Created by qi_liang on 2018/6/9. |
||||
*/ |
||||
public class OsUtil { |
||||
static Properties properties = System.getProperties(); |
||||
|
||||
|
||||
//
|
||||
// public OsUtil(OperatingSystem os){
|
||||
// this.os = os;
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 操作系统
|
||||
// */
|
||||
// public static String getArch(){
|
||||
// return os.getArch();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// *
|
||||
// */
|
||||
// public static String getCpuEndian(){
|
||||
// return os.getCpuEndian();
|
||||
// }
|
||||
//
|
||||
// public static String getDateModel(){
|
||||
//
|
||||
// return os.getDataModel();
|
||||
// }
|
||||
|
||||
public static OsInfo getInfo(){ |
||||
OsInfo osInfo = new OsInfo(); |
||||
osInfo.setOsVersion(properties.getProperty("os.version")); |
||||
osInfo.setOsName(properties.getProperty("os.name")); |
||||
osInfo.setArch(properties.getProperty("os.arch")); |
||||
return osInfo; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,38 @@ |
||||
package org.panda.code.uitl.os.mode; |
||||
|
||||
/** |
||||
* @author qi |
||||
* Created by qi_liang on 2018/6/10. |
||||
*/ |
||||
public class JvmInfo { |
||||
//版本号
|
||||
private String version; |
||||
//总内存
|
||||
private Long totalMemory; |
||||
//可以使用处理器个数
|
||||
private Integer cupNum; |
||||
|
||||
public String getVersion() { |
||||
return version; |
||||
} |
||||
|
||||
public void setVersion(String version) { |
||||
this.version = version; |
||||
} |
||||
|
||||
public Long getTotalMemory() { |
||||
return totalMemory; |
||||
} |
||||
|
||||
public void setTotalMemory(Long totalMemory) { |
||||
this.totalMemory = totalMemory; |
||||
} |
||||
|
||||
public Integer getCupNum() { |
||||
return cupNum; |
||||
} |
||||
|
||||
public void setCupNum(Integer cupNum) { |
||||
this.cupNum = cupNum; |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package org.panda.code.uitl.os.mode; |
||||
|
||||
/** |
||||
* @author qi |
||||
* Created by qi_liang on 2018/6/10. |
||||
*/ |
||||
public class OsInfo { |
||||
|
||||
//操作系统名称
|
||||
private String osName; |
||||
//操作系统版本号
|
||||
private String osVersion; |
||||
//操作系统架构
|
||||
private String arch; |
||||
|
||||
public String getOsName() { |
||||
return osName; |
||||
} |
||||
|
||||
public void setOsName(String osName) { |
||||
this.osName = osName; |
||||
} |
||||
|
||||
public String getOsVersion() { |
||||
return osVersion; |
||||
} |
||||
|
||||
public void setOsVersion(String osVersion) { |
||||
this.osVersion = osVersion; |
||||
} |
||||
|
||||
public String getArch() { |
||||
return arch; |
||||
} |
||||
|
||||
public void setArch(String arch) { |
||||
this.arch = arch; |
||||
} |
||||
} |
Loading…
Reference in new issue