PHP数据格式转java php转成java

PHP代码变成java代码

php代码没几行,信息量很大,翻译成java代码行数量比较大。仅提供思路和php代码解释。

成都创新互联公司服务项目包括濂溪网站建设、濂溪网站制作、濂溪网页制作以及濂溪网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,濂溪网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到濂溪省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

---------------

?php 

$appid = "123"; //数组里面的值,id。

$apikey = "456"; //数组里面的值,为加密密钥。

$secretKey ="789"; //数组里面的值,安全密钥。

$timestamp = time(); ////数组里面的值,获得当前时间。

//UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和。

//echo输出$timestamp变量值,例如输出了1389379960

echo $timestamp;  

//定义数组。以键值对方式存储。

//'appid' 'apikey' 'secretkey' 'timestamp'是key,键。

//$appid $apikey, $secretKey $timestamp是value,值。

$params = array('appid'=$appid, 'apikey'=$apikey, 'secretkey'=$secretKey, 'timestamp'=$timestamp);

//对数组键值进行升序排序。排序结果为apikey appid secretkey timestamp

ksort($params);

//拼接数组中的参数,并且用encoded编码。

//http_build_query -- 生成 url-encoded 之后的请求字符串。当数组没有写下标时,就会用第二个参数结合当前默认下标当前缀。

//$param_uri变量值,结果为apikey=456appid=123secretkey=789×tamp=1389379498

$param_uri = http_build_query($params,'','');

echo $param_uri;   //echo输出结果为apikey=456appid=123secretkey=789×tamp=1389379498

//先使用调用hash_hmac方法加密,HMAC-SHA1算法。

//$secretKey为安全密钥,$param_uri为要加密的明文。'sha1'是HMAC-SHA1算法。

//再调用base64_encode方法加密,base64_encode 使用 MIME base64 对数据进行编码。

$sig = base64_encode(hash_hmac('sha1', $param_uri, $secretKey));

?

java:

1、用hashmap存储元素,键值对方式。

MapString, String hashMap = new HashMapString, String(){

{

put("appid", "123");

put("apikey", "456");

put("secretKey", "789");

put("timestamp", "当前UNIX 时间戳,秒数,java中获取");

}            

};

2、java中可以通过Timestamp获得UNIX 时间戳。

3、然后对hashmap进行升序排序。

4、然后写一个方法遍历hashmap,拼接成字符串格式为apikey=456appid=123secretkey=789timestamp=1389379498

然后对该字符串进行encoded编码,输出格式为apikey=456appid=123secretkey=789×tamp=1389379498

5、通过java中HMAC-SHA1算法加密该字符串,$secretKey为安全密钥。

6、再通过base64_encode加密第5步产生的字符串。这是最终sig结果。

php 如何将图片转换成java中Byte[]的

按照你的要求编写的Java程序如下:( 要注意的地方见语句后面的注释)

import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class ImageWithArray { public static void main(String[] args) {  // 读取图片到BufferedImage  BufferedImage bf = readImage("c:\\tmp\\6\\female.png");//这里写你要读取的绝对路径+文件名  // 将图片转换为二维数组  int[][] rgbArray1 = convertImageToArray(bf);  // 输出图片到指定文件  writeImageFromArray("c:\\tmp\\2.png", "png", rgbArray1);//这里写你要输出的绝对路径+文件名  System.out.println("图片输出完毕!"); } public static BufferedImage readImage(String imageFile){  File file = new File(imageFile);  BufferedImage bf = null;  try {   bf = ImageIO.read(file);  } catch (IOException e) {   e.printStackTrace();  }  return bf; } public static int[][] convertImageToArray(BufferedImage bf) {  // 获取图片宽度和高度  int width = bf.getWidth();  int height = bf.getHeight();  // 将图片sRGB数据写入一维数组  int[] data = new int[width*height];  bf.getRGB(0, 0, width, height, data, 0, width);  // 将一维数组转换为为二维数组  int[][] rgbArray = new int[height][width];  for(int i = 0; i  height; i++)   for(int j = 0; j  width; j++)    rgbArray[i][j] = data[i*width + j];  return rgbArray; } public static void writeImageFromArray(String imageFile, String type, int[][] rgbArray){  // 获取数组宽度和高度  int width = rgbArray[0].length;  int height = rgbArray.length;  // 将二维数组转换为一维数组  int[] data = new int[width*height];  for(int i = 0; i  height; i++)   for(int j = 0; j  width; j++)    data[i*width + j] = rgbArray[i][j];  // 将数据写入BufferedImage  BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);  bf.setRGB(0, 0, width, height, data, 0, width);  // 输出图片  try {   File file= new File(imageFile);   ImageIO.write((RenderedImage)bf, type, file);  } catch (IOException e) {   e.printStackTrace();  } }}

运行结果:

图片输出完毕!

原图:

输出图:

PHP代码转为java代码

没法转的,这个php中调用了不少外部对象,没人能猜到那些是什么内容的。


当前标题:PHP数据格式转java php转成java
文章地址:http://myzitong.com/article/dossdod.html