用Socket传输生成验证码
第一步:使用webService生成两个工具包


生成工具包:

将生成的两个包放到项目中去。然后封装随机字符的类和生成验证码字节的类。
随机类:
public class FontsFactory {
private static RandomFontsWebServiceSoap soap = new RandomFontsWebService().getRandomFontsWebServiceSoap();
/**
*随机生成charLength个字符
* @param charLength
* @return String
*/
public static String getChars(Integer charLength){
return soap.getCharFonts(charLength).getStr();
}
/**
*随机生成chineseLength个汉字
* @param chineseLength
* @return String
*/
public static String getChinese(Integer chineseLength){
return soap.getChineseFonts(chineseLength).getStr();
}
}
生成验证码字节类:
public class ValidateCodeFactory {
private static ValidateCodeWebServiceSoap soap = new ValidateCodeWebService().getValidateCodeWebServiceSoap();
/**
* 生成字母数字验证码
* @param chars
* @return byte[]
*/
public static byte[] getCharsVCode(String chars){
return soap.enValidateByte(chars);
}
/**
* 生成汉字验证码
* @param chinese
* @return byte[]
*/
public static byte[] getChineseVCode(String chinese){
return soap.cnValidateByte(chinese);
}
}
Socket服务类:
public class SocketImpl {
private ServerSocket serverSocket;
private Socket socket;
public SocketImpl(Integer port){
try {
serverSocket = new ServerSocket(port);
System.out.println("服务已启动......");
} catch (IOException e) {
System.err.println("IO错误");
}
}
public void start(){
try {
while (true){
socket = serverSocket.accept();
SocketThread socketThread = new SocketThread(socket);
new Thread(socketThread).start();
}
} catch (IOException e) {
System.err.println("IO错误");
}
}
}
解析请求、回复类:
class SocketThread extends Thread{
Socket socket;
public SocketThread(Socket socket){
this.socket = socket;
}
@Override
public void run() {
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStreamReader inputStreamReader = null;
try {
//获得输出流
outputStream = socket.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
//获得输入流
inputStreamReader = new InputStreamReader(socket.getInputStream());
StringBuffer stringBuffer = new StringBuffer();
char[] chars = new char[1024];
int len = inputStreamReader.read(chars);
//获取请求信息
stringBuffer.append(new String(chars,0,len));
//解析请求资源
String path = stringBuffer.substring(stringBuffer.indexOf("/") + 1, stringBuffer.indexOf(" HTTP/1.1")) ;
Map map = null ;
//有参数就解析参数
if (path.indexOf("?")!=-1){
map = new HashMap();
String param = path.substring(path.indexOf("?") + 1);
path = stringBuffer.substring(stringBuffer.indexOf("/") + 1, stringBuffer.indexOf("?"));
String[] split = param.split("&");
for (String s : split) {
String[] split1 = s.split("=");
map.put(split1[0], URLDecoder.decode(split1[1],"utf-8"));
}
}
//判断请求的资源
if ("codeImage".equals(path)){
if (map != null){//有参返回的二维码
outputStream.write(Result.getCodeImage((String) map.get("content")));
}else//默认返回二维码
outputStream.write(Result.getCodeImage("http://hxb19990523.top:8080/HTML/index.html"));
}else if ("vCode".equals(path)){
if (map != null){//有参返回
//判断验证码类型
boolean flag = "cn".equals(map.get("type"))?true:false;
//没有设置验证码长度默认为4
Integer length = map.get("len")!=null? Integer.parseInt(map.get("len").toString()) : 4;
if (flag) {
//汉字验证码
outputStream.write(ValidateCodeFactory.getChineseVCode(FontsFactory.getChinese(length)));
} else {
//字母、数字验证码
outputStream.write(ValidateCodeFactory.getCharsVCode(FontsFactory.getChars(length)));
}
}else {//默认返回
int i = new Random().nextInt(2);
if (i==0)outputStream.write(ValidateCodeFactory.getCharsVCode(FontsFactory.getChars(4)));
else outputStream.write(ValidateCodeFactory.getChineseVCode(FontsFactory.getChinese(4)));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStreamWriter.close();
outputStream.close();
inputStreamReader.close();
} catch (IOException e) {
System.err.println("IO错误");
}
}
}
}
测试类:
public class MainTest {
public static void main(String[] args) {
new SocketImpl(80).start();
}
}
有参返回验证码(http://localhost/vCode?type=en、http://localhost/vCode?len=5、http://localhost/vCode?type=cn&len=5):

无参返回验证码(http://localhost/vCode):

OVER~

浙公网安备 33010602011771号