gpt4 book ai didi

java - 我们可以使用小程序在客户机中搜索文件吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:35:59 25 4
gpt4 key购买 nike

我是一名 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之后,您必须使用以下命令制作 key 证书 keytool -genkey -alias AppletFileSearch -validity 365将有 10 或 11 个步骤(将采用密码、名字、姓氏示例在 url 中给出: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)。并从浏览器运行,让我们尽情享受吧。

关于java - 我们可以使用小程序在客户机中搜索文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com