You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
6.3 KiB
170 lines
6.3 KiB
package com.yipin.liuwanr; |
|
|
|
import java.io.BufferedInputStream; |
|
import java.io.BufferedReader; |
|
import java.io.ByteArrayOutputStream; |
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.IOException; |
|
import java.io.InputStreamReader; |
|
import java.io.PrintWriter; |
|
import java.net.Socket; |
|
import java.security.InvalidKeyException; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.util.ArrayList; |
|
import java.util.zip.CRC32; |
|
|
|
import javax.crypto.BadPaddingException; |
|
import javax.crypto.Cipher; |
|
import javax.crypto.IllegalBlockSizeException; |
|
import javax.crypto.NoSuchPaddingException; |
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import org.apache.commons.codec.binary.Base64; |
|
import org.apache.commons.lang3.ArrayUtils; |
|
|
|
import com.aliyun.oss.OSSClient; |
|
import com.yipin.liuwanr.entity.Speed; |
|
import com.yipin.liuwanr.helper.RandomUtil; |
|
import com.yipin.liuwanr.helper.SensorUtil; |
|
|
|
public class UserTest { |
|
public static void main(String[] args) { |
|
/* |
|
String password = "123456789"; |
|
String encoded = encryptePassword(password); |
|
System.out.println(RandomUtil.parsePassword(encoded)); |
|
System.out.println(RandomUtil.getMD5Bytes(password.getBytes())); |
|
System.out.println(GetFileSign()); |
|
UploadImageTest(); |
|
testSocketServer(); |
|
System.out.println(PushHelper.updateIMUser("16a00afdb0e94ab5a2f04b3b4ad20d0b", "互动消息", "https://lwavatars.oss-cn-shenzhen.aliyuncs.com/interaction.png")); |
|
System.out.println(PushHelper.watchIMUser("[\"16a00afdb0e94ab5a2f04b3b4ad20d0b\"]")); |
|
*/ |
|
testStepCount(); |
|
} |
|
|
|
public static void testStepCount(){ |
|
ArrayList<Speed> sdata = new ArrayList<Speed>(); |
|
for(int i=0;i<100;i++){ |
|
Speed s = new Speed(new Float(Math.random()*10), new Float(Math.random()*10), new Float(Math.random()*10) ); |
|
sdata.add(s); |
|
} |
|
System.out.println(SensorUtil.countMoves(sdata)); |
|
} |
|
|
|
private static void UploadImageTest(){ |
|
OSSClient ossClient = new OSSClient("oss-cn-shenzhen.aliyuncs.com", "LTAIHIkGqaILObBm", "QDTxKMrfDPeJ3bsr3AqjYHwnlL6PdM"); |
|
ossClient.putObject("d814b6d1bbab4a84afcbfb765e231024","test.jpg",new File("/Users/ptdcxywzptdq/Downloads/100EOS5D/T0QtWInMhK33BexwBkE2JA.JPG")); |
|
} |
|
|
|
private static void testSocketServer(){ |
|
try { |
|
// 1、创建客户端Socket,指定服务器地址和端口 |
|
// Socket socket=new Socket("127.0.0.1",5200); |
|
Socket socket = new Socket("www.liuwanr.cn", 9123); |
|
System.out.println("客户端启动成功"); |
|
// 2、获取输出流,向服务器端发送信息 |
|
// 向本机的52000端口发出客户请求 |
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
|
// 由系统标准输入设备构造BufferedReader对象 |
|
PrintWriter write = new PrintWriter(socket.getOutputStream()); |
|
// 由Socket对象得到输出流,并构造PrintWriter对象 |
|
//3、获取输入流,并读取服务器端的响应信息 |
|
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
|
// 由Socket对象得到输入流,并构造相应的BufferedReader对象 |
|
String readline; |
|
readline = br.readLine(); // 从系统标准输入读入一字符串 |
|
while (!readline.equals("end")) { |
|
// 若从标准输入读入的字符串为 "end"则停止循环 |
|
write.println(readline); |
|
// 将从系统标准输入读入的字符串输出到Server |
|
write.flush(); |
|
// 刷新输出流,使Server马上收到该字符串 |
|
System.out.println("Client:" + readline); |
|
// 在系统标准输出上打印读入的字符串 |
|
System.out.println("Server:" + in.readLine()); |
|
// 从Server读入一字符串,并打印到标准输出上 |
|
readline = br.readLine(); // 从系统标准输入读入一字符串 |
|
} // 继续循环 |
|
//4、关闭资源 |
|
write.close(); // 关闭Socket输出流 |
|
in.close(); // 关闭Socket输入流 |
|
socket.close(); // 关闭Socket |
|
} catch (Exception e) { |
|
System.out.println("can not listen to:" + e);// 出错,打印出错信息 |
|
} |
|
} |
|
|
|
private static String GetFileSign(){ |
|
String ret = ""; |
|
File file; |
|
FileInputStream fis; |
|
ByteArrayOutputStream baos = null; |
|
BufferedInputStream bis = null;; |
|
try { |
|
file = new File("/Users/ptdcxywzptdq/Downloads/100EOS5D/T0QtWInMhK33BexwBkE2JA.JPG"); |
|
fis = new FileInputStream(file); |
|
baos = new ByteArrayOutputStream((int)file.length()); |
|
bis = new BufferedInputStream(fis); |
|
byte[] buffer = new byte[1024]; |
|
int len = 0; |
|
while(true){ |
|
len = bis.read(buffer); |
|
if(len == -1){ |
|
break; |
|
} |
|
baos.write(buffer); |
|
} |
|
return Base64.encodeBase64URLSafeString(RandomUtil.getMD5Bytes(baos.toByteArray())); |
|
} catch (FileNotFoundException e) { |
|
e.printStackTrace(); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} finally{ |
|
try { |
|
baos.close(); |
|
bis.close(); |
|
} catch (IOException e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
return ret; |
|
} |
|
|
|
private static String encryptePassword(String password){ |
|
byte[] content = password.getBytes(); |
|
CRC32 crcEncoder = new CRC32(); |
|
crcEncoder.update(content); |
|
String crcStr = Long.toHexString(crcEncoder.getValue()); |
|
System.out.println("CRC:"+crcStr); |
|
byte[] crc = crcStr.getBytes(); |
|
byte[] high = new byte[4]; |
|
byte[] low = new byte[4]; |
|
System.arraycopy(crc, 0, high, 0, 4); |
|
System.arraycopy(crc, 4, low, 0, 4); |
|
String pass = ""; |
|
try { |
|
SecretKeySpec sks = new SecretKeySpec(ArrayUtils.addAll(crc, crc), "AES"); |
|
Cipher cp = Cipher.getInstance("AES"); |
|
cp.init(Cipher.ENCRYPT_MODE, sks); |
|
byte[] encryptedpass = cp.doFinal(content); |
|
System.out.println("AES:" + Base64.encodeBase64URLSafeString(encryptedpass)); |
|
pass = Base64.encodeBase64URLSafeString(ArrayUtils.addAll(ArrayUtils.addAll(high, encryptedpass), low)); |
|
System.out.println("BASE:" + new String(pass)); |
|
} catch (NoSuchAlgorithmException e) { |
|
e.printStackTrace(); |
|
} catch (NoSuchPaddingException e) { |
|
e.printStackTrace(); |
|
} catch (InvalidKeyException e) { |
|
e.printStackTrace(); |
|
} catch (IllegalBlockSizeException e) { |
|
e.printStackTrace(); |
|
} catch (BadPaddingException e) { |
|
e.printStackTrace(); |
|
} |
|
System.out.println(pass); |
|
return pass; |
|
} |
|
}
|
|
|