2013-02-25 53 views
5

你好傢伙我是一名PHP開發人員。我有一個要求,那就是客戶機中會有一個特定的文件,如果這個文件存在,那麼這個用戶就可以登錄到這個網站上。我可以用下面給出的代碼獲取文件是否存在:我們可以使用applet在客戶機器中搜索文件嗎?

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; 
} 

}

,但我怎麼能在我的項目實現這一點,併發送該響應的服務器,使得最終用戶可以能夠登錄或不。 在此先感謝。

+0

你發佈的代碼是applet嗎? – Samy 2013-02-25 07:02:47

+0

這不是小程序。這只是java代碼。我必須將此代碼轉移到小程序。 – Banshi 2013-02-25 07:26:10

回答

1

應該能夠通過一個小程序,更具體地說,一個signed applet做你需要的。 This教程應指向正確的方向,以便您可以讓您的小程序訪問用戶的本地文件系統。

0

Unsigned applets訪問客戶端資源(如本地文件系統等)。爲了打開該文件,您需要部署一個已簽名的applet(並且我認爲用戶會被警告該applet將以不受限制的權限運行)。替代方法,您可以改爲執行TSL/SSL client authentication

4

我的問題的小程序是在下面:

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 在命令提示符下此源,然後使用命令 罐子CVF AppletFileSearch使其jar文件。 jar AppletFileSearch.class 之後,您必須使用命令 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)。並從瀏覽器中運行並讓其享受。

相關問題