PHP&Java(1)

 更新时间:2016年11月25日 16:33  点击:1281

    Mark Nold
Joost Soeterbroek
 
The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.

Windows Installation


The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.xin directory is in your path. This will make compiling your Java Classes easier. On Win9x add

PATH=%PATH%;C:jdk1.2.2in


to your autoexec.bat. On NT add

;C:jdk1.2.2in


to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.
Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file
 

[java]
extension=php_java.dll
java.library.path=c:webphp4extensionsjava.class.path="c:webphp4extensionsjdk1.2.2php_java.jar;c:myclasses"
     页面(或者servlet)中发送动态产生的图像?这篇技巧告诉你如何做。要运行这里的代码,你需要一个Tomcat或者其他支持JSP 1.1的web服务器。
  当一个web页面带有image/jpeg (或者其他的图像格式)的MIME类型被发送时,你的浏览器将那个返回结果当作一个图像,然后浏览器显示图像,作为页面的一部分或者完全作为图像自身。要为你的jsp页面设置MIME类型,你需要设置页面的contentType属性: 
   
  然后你需要创建一个BufferedImage绘制你的动态图像: 
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  创建完一个BufferedImage后,你需要得到图形环境进行绘制,一个Graphics或者Graphics2D对象: 
  Graphics g = image.getGraphics();
  // or 
  Graphics2d g2d = image.createGraphics(); 
   从现在起你就可以绘制图像内容了。对图形环境绘制就会画到BufferedImage。最开始这个图像都是黑色的,因此用你希望的背景颜色填充图像是一个不错的主意,然后,当你完成图像的绘制,你需要dispose图形环境: 
  g.dispose();
  // or 
  g2d.dispose(); 

 一旦完成图像的绘制,你在response中返回那个图像。你可以使用非标准的com.sun.image.codec.jpeg包中的JPEGImageEncoder类编码图像,或者如果你使用JDK1.4,你可以使用标准的ImageIO类。在使用JPEGImageEncoder时有一个技巧,你必须从ServletResponse取来ServletOutputStream而不能使用隐含的JSP输出变量out。
  ServletOutputStream sos = response.getOutputStream();
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
  encoder.encode(image);
  // or 
  ImageIO.write(image, "JPEG", out); 
  这里有一个从所有的可能方案中(例如g.dispose();或者g2d.dispose();)选取的一个完整的范例.这个例子使用Graphics对象绘制一个随机的多边形,图像通过JPEGImageEncoder绘制,你可以自由设置多边形的顶点数得到更复杂的形状,换言之,有更多顶点和边。
  要运行这个范例,将从""之间的jsp代码放到一个名为image.jsp的文件中,将那个文件放到你的web服务器可以找到的地方,在使用Tomcat的情况下是ROOT目录,启动Tomcat,访问http://localhost:8080/image.jsp 
 <%@ page contentType="image/jpeg" 
     nbsp;用php生成excel文件 
哈哈,今天又学一招。php生成excel文档太简单了,估计大家都会用到,所以
共享出来。
大家来看代码:
<?
         header("Content-type:application/vnd.ms-excel");
         header("Content-Disposition:filename=test.xls");
         echo "test1	";
         echo "test2	
";
         echo "test1	";
         echo "test2	
";
         echo "test1	";
         echo "test2	
";
         echo "test1	";
         echo "test2	
";
         echo "test1	";
         echo "test2	
";
         echo "test1	";
         echo "test2	
";
?>
在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel
文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到
最终的结果,怎么样不错吧。
其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每
一列数据结束后加	,每一行数据结束后加
的方法echo出来,在php的开头
用header("Content-type:application/vnd.ms-excel");表示输出的是
excel文件,用header("Content-Disposition:filename=test.xls");表
示输出的文件名为text.xls。这样就ok了。
我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型
文件方面就更加方便了。
 
Yorgo Sun (yorgo@163.net)
2000/11/14
欢迎转载,但请保证文档完整,通知作者。谢谢 :-)

     P>         网上很的免费支持PHP的个人主页空间现在不少,这时可能会想到来个上传什么的,但毕竟属于免费的,当然会有很多的限制,不允许上传,这个很正常,有的连Mail()函数都不给用呢。遇到这种情况,也不是没有办法上传的哦,PHP支持强大的socket,当然少不了直接操作ftp了,看看函数表上就有了,通过ftp实现文件上传。不是很好,我们上传主页不是用ftp上传吗。
     实现代码如下:
//upload.php
<html>
<body>
<form enctype="multipart/form-data" action=upload.php method=post>  
上传文件:  <input type=file name=upload_file>  
<input type=submit name=action value=OK>  
</form>  
<?php  
$ftp_server="ftp服务器(最好用IP)";  
$ftp_username="ftp用户名";  
$ftp_password="登录密码";  
$ftp_dir="目录(如设置为 /)"; 
if ($action == "OK")  
{  
  $con = ftp_connect($ftp_server);  //连接ftp
  ftp_login($con, $ftp_username, $ftp_password); //用户登录
  $suss=$con;
  ftp_chdir($suss, $ftp_dir);  //cd到指定目录
  $ok_code = ftp_put($suss, $upload_file_name, $upload_file, FTP_BINARY); //put文件 
 
  if ($ok_code == 1)  
  {  
      echo "文件上传成功!
";  
  }  
  else  
  {  
     echo "文件上传失败!
";  
  }  
      ftp_quit($suss);  // 关闭ftp连接
}  
?>  
</body>
</html>

     P>Java语言功能强大,因此在许多情况下在php中来调用Java的功能将十分有用。在php中调用Java语言有两种方法,一种是使用php中的Java扩展模块,另一种是使用minij2ee应用服务器提供的SJOP协议实现。下面我们来比较一下这两种方法各自的特点。
1.php的Java模块
php发布版中包含一个Java扩展模块,可以用来调用Java对象,例如:
<?php
$system=new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")." <br>
";
?>
使用这种方法的优点是比较方便,只要用new Java()来创建一个Java对象,就可以同php类一样来调用Java对象。但是这种方法也有以下明显的缺点:
1.由于php的Java模块根据php的数据类型选择最适合的Java方法,因此无法调用Java过载的函数。
2.php的Java模块将在当前Web Server的进程中载入JVM(Java虚拟机),因此系统开销极大,影响Web Server进程的执行效率。
3.在某些操作系统和Web Server环境中,php的Java模块将使Web Server进程僵死。见http://www.php.net/bugs.php?id=6122。
由于这些原因,php的Java模块一直无法应用到实际的的软件系统中。
2.minij2ee应用服务器SJOP协议实现
在介绍minij2ee应用服务器SJOP协议实现之前,先简单介绍一下minij2ee应用服务器。minij2ee应用服务器是第一款支持php的J2EE应用服务器产品,使php能够用于开发企业级应用系统。SJOP全称是Sample Java ORB Protocol(简单Java对象请求代理协议),是一种简单高效的对象请求代理协议。比如:
<?php
$conn=minij2ee_fetch_connection();
print "Java version=".minij2ee_callstatic_javaobj($conn,"java.lang.System","getProperty","java.lang.String","java.version")." <br>
";
?>
minij2ee应用服务器实现SJOP协议的主要目的是使php中能够访问EJB企业级组件,因此minij2ee提供了一个EJB-PHP编译器,可以把EJB组件编译成php的类,使php程序中能够方便的调用EJB组件,例如:
<?php
require("Cart.php"); file://Cart.php是编译Cart EJB后生成的Cart EJB的php类定义。
$home=new CartHome(); file://创建EJB的Home接口。
[!--infotagslink--]

相关文章