Fork me on GitHub
EzHomeSixGod


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

设计模式之命令模式

发表于 2018-05-06 |

设计模式之装饰器模式

发表于 2018-05-06 |

设计模式之装饰者模式

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

  1. 装饰者模式介绍?
  2. 装饰者模式的实现?
  3. 举例?
阅读全文 »

设计模式之观察者模式

发表于 2018-05-06 |

设计模式之观察者模式

本篇是设计模式系列博客的第四篇,本篇主要学习设计模式中的第二个行为型模式—观察者模式。

  1. 什么是观察者模式?
  2. 模式的结构?
  3. 模式自定义实现和JDK自带实现?
  4. 推模型和拉模型?
  5. JDK源码分析?
阅读全文 »

设计模式之策略模式

发表于 2018-05-06 |

设计模式之策略模式

本篇是设计模式系列博客的第三篇,本篇主要讲一个设计模式中的一个行为形模式—策略模式。

阅读全文 »

Java 生成Zxing二维码

发表于 2018-05-06 |

java 利用Zxing 生成二维码(不带Logo)

Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码

1、二维码生成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;


public final class MatrixToImageWriter {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

private MatrixToImageWriter() {}


public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}


public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}


public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}

}

然后测试类生成一个二维码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
try {

String content = "https://www.baidu.com";
String path = "C:/Users/Administrator/Desktop/testImage";

MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
File file1 = new File(path,"test.jpg");
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);

} catch (Exception e) {
e.printStackTrace();
}

这样就生成了一个二维码,还是挺简单的,使用可以扫描的工具;因为内容是一个链接所以也没可能会跳转到这个链接。**

2、解析二维码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
BufferedImageLuminanceSource 
import com.google.zxing.LuminanceSource;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public final class BufferedImageLuminanceSource extends LuminanceSource {

private final BufferedImage image;
private final int left;
private final int top;

public BufferedImageLuminanceSource(BufferedImage image) {
this(image, 0, 0, image.getWidth(), image.getHeight());
}

public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
super(width, height);

int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
}

for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((image.getRGB(x, y) & 0xFF000000) == 0) {
image.setRGB(x, y, 0xFFFFFFFF); // = white
}
}
}

this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
this.image.getGraphics().drawImage(image, 0, 0, null);
this.left = left;
this.top = top;
}

@Override
public byte[] getRow(int y, byte[] row) {
if (y < 0 || y >= getHeight()) {
throw new IllegalArgumentException("Requested row is outside the image: " + y);
}
int width = getWidth();
if (row == null || row.length < width) {
row = new byte[width];
}
image.getRaster().getDataElements(left, top + y, width, 1, row);
return row;
}

@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
image.getRaster().getDataElements(left, top, width, height, matrix);
return matrix;
}

@Override
public boolean isCropSupported() {
return true;
}

@Override
public LuminanceSource crop(int left, int top, int width, int height) {
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
}

@Override
public boolean isRotateSupported() {
return true;
}

@Override
public LuminanceSource rotateCounterClockwise() {

int sourceWidth = image.getWidth();
int sourceHeight = image.getHeight();

AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);

BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);

Graphics2D g = rotatedImage.createGraphics();
g.drawImage(image, transform, null);
g.dispose();

int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
}

}

测试:

public static void main(String[] args) throws IOException, NotFoundException {
       MultiFormatReader formatReader = new MultiFormatReader();
       String filePath = "C:/Users/Administrator/Desktop/testImage";
       File file = new File(filePath);
       BufferedImage image = ImageIO.read(file);
       LuminanceSource source = new BufferedImageLuminanceSource(image);
       Binarizer binarizer = new HybridBinarizer(source);
       BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
       Map hints = new HashMap();
       hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
       Result result = formatReader.decode(binaryBitmap,hints);

       System.out.println("result = "+ result.toString());
       System.out.println("resultFormat = "+ result.getBarcodeFormat());
       System.out.println("resultText = "+ result.getText());
   }  

下面会给出生成logo的二维码代码工具.

Java 生成Zxing二维码(带Logo)

发表于 2018-05-06 | 分类于 Java |

java 利用Zxing 生成二维码(带Logo)

Zxing是Google提供的关于条码(一维码、二维码)的解析工具,提供了二维码的生成与解析的方法,现在我简单介绍一下使用Java利用Zxing生成与解析二维码

阅读全文 »

Java类加载器

发表于 2018-05-06 | 分类于 Java |

自定义ClaaLoader加载类文件

类加载器是JVM执行类加载机制的前提,其主要任务为根据一个类的全限定名来读取此类的二进制字节流到JVM内部,然后转换为一个与目标类对应的java.lang.Class对象实例

  1. defineClass();

  defineClass方法 的主要作用是将byte 字节流解析成JVM能够识别的class对象,这个方法意味着 我们不仅仅可以通过class文件去实例化对象,还可以其他方式实例化对象,例如我们通过网络接收到一个类的字节码;defineClass的代码如下:
  
  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
protected final Class<?> defineClass(String name, byte[] b, int off, int len,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
protectionDomain = preDefineClass(name, protectionDomain);
String source = defineClassSourceLocation(protectionDomain);
Class<?> c = defineClass1(name, b, off, len, protectionDomain, source);
postDefineClass(c, protectionDomain);
return c;
}


protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
int len = b.remaining();

// Use byte[] if not a direct ByteBufer:
if (!b.isDirect()) {
if (b.hasArray()) {
return defineClass(name, b.array(),
b.position() + b.arrayOffset(), len,
protectionDomain);
} else {
// no array, or read-only array
byte[] tb = new byte[len];
b.get(tb); // get bytes out of byte buffer.
return defineClass(name, tb, 0, len, protectionDomain);
}
}

protectionDomain = preDefineClass(name, protectionDomain);
String source = defineClassSourceLocation(protectionDomain);
Class<?> c = defineClass2(name, b, b.position(), len, protectionDomain, source);
postDefineClass(c, protectionDomain);
return c;
}

  1. findClass();
      实现类的加载规则,取得要加载类的字节码,通常是和defineClass()一起使用的;查找类,返回java.lang.Class类的实例

  2. loadClass();
      加载类,返回java.lang.Class类的实例

  3. resolveClass();
      连接指定的一个类,如果你想在类被加载到JVM中的时候就被链接(Link),则调用resolveClass()方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.test;

import java.io.*;
import java.lang.reflect.Method;

/**
* \* Created with IntelliJ IDEA.
* \* @author:
* \* @date: 2017/12/14
* \* @time: 10:53
* \* Description:
* \
*/
public class MyClassLoader extends ClassLoader {
//类文件在电脑中的路径
private String classPath;

public String getClassPath() {
return classPath;
}

public void setClassPath(String classPath) {
this.classPath = classPath;
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] bytes =null;

try {
bytes =LoadClassData(name);

} catch (IOException e) {
e.printStackTrace();
}
return super.defineClass(name,bytes,0,bytes.length);
}


//加载类文件二进制流
public byte[] LoadClassData(String name) throws IOException {
File file =getFile(name);
byte[] by =new byte[(int)file.length()];
FileInputStream inputStream =new FileInputStream(file);
inputStream.read(by);
return by;
}

//获取类文件
public File getFile(String name) throws FileNotFoundException {
File dir = new File(classPath);
if(!dir.exists()) throw new FileNotFoundException(classPath+"目录不存在!");
String _classPath = classPath.replaceAll("[//]","/");
int offset = _classPath.lastIndexOf("/");
name = name.replaceAll("[.]","/");
if(offset != -1 && offset < _classPath.length()-1){
_classPath +="/";
}
_classPath += name +".class";
dir = new File(_classPath);
if(!dir.exists()) throw new FileNotFoundException(dir+"不存在!");
return dir;
}
public static void main(String[] args) {
MyClassLoader classLoader =new MyClassLoader();
String classpath="C:\\Users\\Administrator\\Desktop\\ClassLoaderTest\\build\\classes";
classLoader.setClassPath(classpath);
try {
Class c =classLoader.loadClass("com.assist.controller.AssessmentAgenciesController");
Object o=c.newInstance();
Method[] methods =c.getMethods();
for(Method m :methods){
System.out.println(m.getName());
}
System.out.println("obj.class="+o.getClass().getName());
System.out.println("obj.class="+c.getClassLoader().toString());
System.out.println("obj.class="+c.getClassLoader().getParent().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
AssessmentAgencies
toAgenciesDetails
querySettleService
toAssessmentScheme
queryAssessmentScheme
toAssessmentRecord
toAgencies
queryWaitAudit
passScheme
toAddAgencies
addAgencies
editAgencies
editAgencies
deleteDistrict
uploadFile
checkData
toSettleService
settleUpdate
toNewsList
queryNewsList
assessmentRecord
exportAgencies
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
obj.class=com.assist.controller.AssessmentAgenciesController
obj.class=sun.misc.Launcher$AppClassLoader@18b4aac2
obj.class=sun.misc.Launcher$ExtClassLoader@73c6c3b2

Linux自动发布Tomcat项目脚本

发表于 2018-05-06 | 分类于 Linux |

Shell 脚本之自动发布Tomcat项目

鉴于每次测试让发布测试环境的项目,都是手动一系列的操作:连接SSH、CD、RM -RF ../项目、PS -EF|GREP TOMCAT-WEB等繁琐的操作实在蛋疼,后来就想直接自己把这个命令写好放在.sh文件中,手动运行不就行了。

1、Web服务器Linux环境搭建(Tomcat)

2、Shell脚本编写

3、测试

1、Web服务器Linux环境搭建(Tomcat)

1.1、JDK环境配置

Java开发的程序猿们都知道,现在的互联网公司生产环境大多都是Linux服务器,并且用的比较多的大都是CentOS,Red hat系列的Linux系统环境,下面我就以我当前安装的CentOS 7.3来说明一下在该环境下安装Jdk 1.8.1_62版本的操作过程吧,如下所述:

一:首先下载对应CentOS版本的jdk:jdk-8u162-linux-x64.tar.gz ,下载地址为:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html。

二:下载该jdk到本地,并上传到你的CentOS系统的opt临时目录下。

三:在安装自己下载的jdk之前,要先看看新安装的CentOS 7.0系统自带的jdk版本,如果存在最好是卸载掉,然后再安装自己下载的jdk版本,如何卸载linux系统自带的jdk网上有很多例子,相信大家都会操作。

四:新建一个jdk的安装目录,我这里是在/usr/local/下新建了java目录,命令:

1
mkdir /usr/local/java

再把 jdk-8u162-linux-x64.tar.gz压缩包从opt目录下复制到/usr/local/java目录下,命令如下:

1
cp jdk-7u75-linux-x64.tar.gz /usr/local/java

五:解压缩刚才下载的: jdk-8u162-linux-x64.tar.gz包,命令如下:

1
tar xvf  jdk-8u162-linux-x64.tar.gz

六:解压完成后,开始编辑环境变量进入到:

1
2
[root@localhost~]# cd /etc  
[root@localhost etc]# vi profile

在profile文件的末尾加入如下命令:

1
2
3
4
export JAVA_HOME=/usr/local/java/jdk1.8.1_62 
export JRE_HOME=/usr/local/java/jdk1.8.1_62/jre
export PATH=$PATH:/usr/local/java/jdk1.8.1_62/bin
export CLASSPATH=./:/usr/local/java/1.8.1_62/lib:/usr/local/java/1.8.1_62/jre/lib

输入:wq保存并退出,然后再重启CentOS 6.4系统

七:重新启动系统后,输入:java -version,如果看到输出JDK的版本信息就说明你的jdk已经安装成功了。

1.2、SVN 安装

如果使用的是GNU/Linux基于RPM,然后使用yum命令进行安装。安装成功后,执行的svn - version命令。

1
2
3
4
5
[root@localhost ~]# yum install subversion

[root@localhost ~]$ svn --version
svn, version 1.6.11 (r934486)
compiled Jun 23 2012, 00:44:03

如果使用的是基于Debian GNU/Linux的命令进行安装然后用apt:

1
2
3
4
5
[root@localhost]$ sudo apt-get install subversion

[root@localhost]$ svn --version
svn, version 1.7.5 (r1336830)
compiled Jun 21 2013, 22:11:49

1.3、Maven 安装

1.首先到Maven官网下载安装文件,目前最新版本为3.5.3,下载文件为apache-maven-3.5.3-bin.tar.gz,下载可以使用wget命令。

2.进入下载文件夹,找到下载的文件,运行如下命令解压:

1
tar -xvf apache-maven-3.5.3-bin.tar.gz

解压后的文件夹名为apache-maven-3.5.3。

3.使用mv命令将apache-maven-3.5.3文件夹拷贝到自己指定的文件夹,比如/usr/local/下

1
mv -rf apache-maven-3.0.3 /usr/local/

4.配置环境变量,编辑/etc/profile文件,添加如下代码

1
2
3
MAVEN_HOME=/usr/local/apache-maven-3.0.3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin

5.保存文件,并运行如下命令使环境变量生效

1
source /etc/profile

6.在控制台输入如下命令,如果能看到Maven相关版本信息,则说明Maven已经安装成功

1
mvn -v

2、Shell 脚本编写

脚本路径:/java_tool/deployerTool.sh
启动带参数(参数是重新发布的哪个系统。这里举例gov,shcool)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash

#获取启动参数即系统
echo "参数为$1"

#判断是哪个系统
if [ "$1" == "gov" ]

then

echo "部署->gov"

#定义gov的Tomcat服务器路径
tpath="/webpub/tomcat-gov-test"
#定义gov svn文件路径
src_path="/svn_source/gov/test"

war_path="${src_path}"

elif [ "$1" == "school" ]

then

echo "部署->school"

tpath="/webpub/tomcat-school-test"

src_path="/svn_source/school/test"

war_path="${src_path}/web"

else

echo "暂不支持"

exit 0

fi

echo "tomcat-path:${tpath}"

echo "svn-path:${src_path}"

echo "war-path:${war_path}"

#exit 0
#进入源码文件
cd $src_path
#执行svn update更新源码文件
svn update

#执行maven clean 任务
maven clean

#maven 编译打包
maven build package -Denv=test

#查找当前重新部署的服务器是否在运行状态
pidlist=`ps -ef|grep ${tpath}|grep -v "grep"|awk '{print $2}'`

#结束当前服务器
function stop(){

if [ "$pidlist" == "" ]

then

echo "----${tpath} 已经关闭----"

# exit 0

else

echo "${tpath}进程号 :$pidlist"

kill -9 $pidlist

echo "KILL $pidlist:"

fi

}

stop

pidlist2=`ps -ef|grep ${tpath}|grep -v "grep"|awk '{print $2}'`

if [ "$pidlist2" == "" ]

then

echo "----关闭${tpath}成功----"

else

echo "----关闭tomcat-test失败----"

fi

#删除服务器下面旧的web 文件
rm -rf ${tpath}/webapps/*

#复制war包到tomcat下面
cp ${war_path}/build/libs/* ${tpath}/webapps/ROOT.war

#进入tomcat bin 路径
cd ${tpath}/bin

#启动服务器
./startup.sh

3、测试

执行/java_tool/publish.sh gov 如果终端输出部署打印的日志文件说明成功。

Java并发之Synchronized

发表于 2018-05-06 |

Java并发编程系列之synchronized

接上一篇《Java并发之volatile》,这是第二篇,说的是关于并发编程的synchronized元素。

  1. synchronized使用方法
  2. synchronized 实现原理
  3. 锁优化
  4. 总结
阅读全文 »

Java并发之Volatitle

发表于 2018-05-06 | 分类于 Volatile |

Java 并发之Volatile

上一篇简单的了解了Java中的常用阻塞队列,今天来讲讲Java中经常使用到的另一个比较难懂的关键字Volatile,这个关键字之所以难懂,是因为他在Java字节码层面做了些动作。今天就来看看。

再讲Volatile之前,这里先让大家了解下Java 应该容易搞混淆的两个概念,之前笔者也没有弄清楚也是后面查资料,这里也一并提出来。

  1. Java内存模型(JMM)
  2. Jvm内存模型
  3. Volatile语义
  4. 使用场景
  5. 原理浅析
  6. 总结
阅读全文 »
12
EzHomeSixGod

EzHomeSixGod

理想公式:try {t = "love";return t;} catch (Exception e) {t = "money";return t;} finally {t = "money and love";return t;}

18 日志
7 分类
12 标签
GitHub 简书
© 2018 EzHomeSixGod
本站访客数: