我是一名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 -alias AppletFileSearch -validity 365
将有10或11个步骤(将在网址中给出密码,名字,姓氏示例:http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm).Again使用命令进行最终签名
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).并从浏览器运行,让我们享受.