很多站长朋友们都不太清楚php怎么转java,今天小编就来给大家整理php怎么转java,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP代码变成java代码 2、 PHP代码转为java代码 3、 php 如何将图片转换成java中Byte[]的 4、 如何将PHP转换成JAVA 5、 php示例怎么转java? PHP代码变成java代码php代码没几行,信息量很大,翻译成java代码行数量比较大。仅提供思路和php代码解释。
---------------
<?php
$appid = "123"; //数组里面的值,id。
$apikey = "456"; //数组里面的值,为加密密钥。
$secretKey ="789"; //数组里面的值,安全密钥。
$timestamp = time(); ////数组里面的值,获得当前时间。
//UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 0 到当前时间的秒数之和。
//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存储元素,键值对方式。
Map<String, String> hashMap = new HashMap<String, 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代码没法转的,这个php中调用了不少外部对象,没人能猜到那些是什么内容的。
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的工具。。。
php示例怎么转java?/**
* 生成签名
* @param string timestamp 时间戳
* @param string appSecret 合作商开发者密钥
* @param string nonce 随机字符串
* @return string
*/
public String makeSignature (String timestamp,String appSecret,String nonce) {
String[] tmpArr = {timestamp, nonce, appSecret};
// 按值升序排序
Arrays.sort(tmpArr)
// 数组拼接为字符串
// 调用md5方法
return signature;
}
其他的都是方法调用, 根据需要编写就行
关于php怎么转java的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php怎么转java php怎么转换成pdf的详细内容...