我是一名PHP开发人员。我有一个要求,在客户端机器中会有一个特定的文件,如果这个文件存在,那么用户就可以登录网站。我可以通过使用下面给出的代码来获得文件的存在:
import java.io.File;
class FileSearchFirstOrder{
public static void main(String args[])
{
boolean isExistP = false;
File volumes = new File("/Volumes");
File files[] = volumes.listFiles();
for(File f: files)
{
//System.out.println("Current File -> " + f.getPath());
isExistP = parseAllFiles(f.getPath());
if(isExistP == true)
break;
}
if(isExistP == true)
System.out.println("I got the desire file Please continue.");
else
System.out.println("Sorry! I can not find the desire file Please try again leter:(");
}
public static boolean parseAllFiles(String parentDirectory)
{
boolean isExistPC = false;
try
{
File[] filesInDirectory = new File(parentDirectory).listFiles();
for(File f : filesInDirectory)
{
if (f.getName().toString().equals("key.txt"))
{
//System.out.println("Current File ->" + f.getName());
isExistPC = true;
}
}
}
catch(Exception e)
{
}
return isExistPC;
}
}
但是,我如何在我的项目中实现这一点,并将此响应发送到服务器,以便最终用户能够登录或不登录。
我的问题的小程序如下:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
public class AppletFileSearch extends JApplet implements ActionListener
{
private JPanel pane = null;
public void init()
{
try{
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public boolean parseAllFiles(String parentDirectory)
{
boolean isExistPC = false;
try
{
File[] filesInDirectory = new File(parentDirectory).listFiles();
for(File f : filesInDirectory)
{
if (f.getName().toString().equals("key.txt"))
{
isExistPC = true;
}
}
}
catch(Exception e){}
return isExistPC;
}
private void jbInit() throws Exception
{
boolean isExistP = false;
File files[] = File.listRoots();
for(File f: files)
{
isExistP = parseAllFiles(f.getPath());
if(isExistP == true)
break;
}
if(isExistP == true)
{
pane = new JPanel();
pane.setBounds(new Rectangle(0, 0, 500, 35));
pane.setLayout(null);
pane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
pane.setBackground(new Color(0, 255,0));
setContentPane(pane);
}
else
{
pane = new JPanel();
pane.setBounds(new Rectangle(0, 0, 500, 35));
pane.setLayout(null);
pane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
pane.setBackground(new Color(255, 0, 0));
setContentPane(pane);
}
}
public void actionPerformed(ActionEvent e) {
}
}
现在使用javac AppletFileSearch.java在命令提示符中,然后使用命令将其创建为jar文件jar cvf AppletFileSearch.jar AppletFileSearch.class之后,您必须使用以下命令制作密钥证书keytool-genkey-别名AppletFileSearch-有效期365将有10或11个步骤(需要密码、名字、姓氏(。例如,在url中给出:http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm)。再次使用命令进行最终签名jarsigner AppletFileSearch.jar AppletFileSearch会有一个提示询问您之前提供的密码。如果你提供了正确的密码,就会收到一条消息,签名者证书将在六个月内过期。
完成这些步骤后,您就可以制作一个html页面,代码如下:
<html>
<title>Run Applet</title>
</head>
<body>
<applet code="AppletFileSearch.class" archive="AppletFileSearch.jar"
width=325 height=325></applet>
</body>
</html
并将该html文件保存为AppletFileSearch.html,并将文件AppletFileSearch.html和AppletFileSearch.jar都保存在服务器中(如apache(。从浏览器中运行,让我们尽情享受。
您应该能够通过小程序,更具体地说,通过签名的小程序来完成您需要的操作。本教程应该为您指明正确的方向,以便您可以让小程序访问用户的本地文件系统。
未签名的小应用程序访问客户端资源,如本地文件系统等。为了打开该文件,您需要部署一个签名的小程序(我认为用户会被警告,小程序将以不受限制的权限运行(。作为替代方法,您可以实现TSL/SSL客户端身份验证。