Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we search for a file in the client machine using applet?

I am a PHP developer. I have a requirement that there will a particular file in the client machine and if this file exists then the user able to log in in the web site. I can get file existence by using the code given below:

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

but how can i implement this in my project and send this response to the server such that the end user can able to log in or not.

like image 786
Banshi Avatar asked Feb 25 '13 06:02

Banshi


1 Answers

The applet of my Question is in below:

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) {
}
}

Now compile this source using javac AppletFileSearch.java in command prompt then make it jar file using the command jar cvf AppletFileSearch.jar AppletFileSearch.class After that you have to make key certificate by using the command keytool -genkey -alias AppletFileSearch -validity 365 There will 10 or 11 steps (that will take password, first name, last name example is given in the url: http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm).Again use the command for final signature jarsigner AppletFileSearch.jar AppletFileSearch There will be a prompt for asking password which you gave previous. If you give correct password the there will be a message the signer certificate will expire within six months.

After making this steps you have make a html page and the code will be like that:

<html>
<title>Run Applet</title>
</head>
<body>
<applet code="AppletFileSearch.class" archive="AppletFileSearch.jar"
       width=325 height=325></applet>
</body>
</html

and save this html file as AppletFileSearch.html and keep both the files AppletFileSearch.html and AppletFileSearch.jar in the server(like apache). And run from the browser and lets enjoy.

like image 155
Banshi Avatar answered Sep 23 '22 14:09

Banshi