Create a trusted applet to access the local file system (1)

Source: Internet
Author: User
Tags readfile

Create a trusted applet to access the local file system (1)
Written by Olexiy & Alexander prokhorenko
Translated by caiyi0903 (willpower), 2004.1.29

It has always been an important topic to create a trusted applet that grants access to the local client file system. Depending on some specifications, many problems can only be executed on the client. Therefore, in many cases, programmers who are not familiar with applet Technology believe that it is impossible to perform operations on client files and search for other operations. However, such a method exists and can be widely used in many aspects. In this article, I will show you an example to create a simple trusted applet that will access a specified local text file (note: you need to use jdk1.3 or a later version ).

First, let me talk about the restrictions we have to make on the client software. I want to declare that the signature applet given in this article is only applicable to clients installed with JDK (version 1.3 or later) or similar plug-ins, and uses browsers later than IE 4 or Netscape 4.75. Clients other than these conditions do not support the requirements described in this article. To create trusted signature applets for these older clients, JDK is required and the javakey tool is used. Because the old method of creating a signature applet is beyond the scope described in this article, it is necessary to explain it here.

Now let's talk about our (programmer) platform. We use the Windows platform and install j2sdk 1.4.1 and Java container Jakarta-Tomcat 4.1.29. If you are using the same software, you can guarantee that all the content described in this article will run perfectly on your platform. Generally, all content described in this article should be able to run on compatible software.

Before coding, I want to explain why we need to find a specific way to do these simple standard tasks, such as reading local files. There is a viewpoint in the Java 1.1 era that it is necessary to transfer some tasks to the client computer with the help of Applet to create a future generation of network computers. However, this approach is not possible because the applet limits security. Developers have also suggested that the applet is a so-called sandbox-a simple position-which cannot reference any system function. With the development of Java technology, trusted applet has been invented, and the "sandbox" security rule in this applet is not included. So how should we take advantage of this opportunity? Next we will find the answer.

In JDK, in addition to javac, we also need two additional utilities-keytool and jarsigner. Keytool is a tool used to create and control a pair of private and public keys. It is used to manage your private keys, that is, the. keystore file. The jarsigner tool is used to sign a jar file. The JAR file contains an applet class that can be used to verify the digital signature distributed with the applet. The entire signature process is very simple. However, we need to prepare for initialization. First, let's create our applet. There is a text field, where we will enter a file name and a large text area to display the file content, A Load button will read the file. The source code of this applet is shown in. Because it is very simple, we add some comments to it. We didn't specifically add some special check boxes and more complex features to it, so the code is easy to read, and the reader can focus on the process of turning it into a trusted applet. In addition, you can even create your own Applet based on your favorite visualization tools (such as Sun ONE studio and JBuilder, however, the source code we provide helps you better understand the entire process of Applet signature.

1:    // TestApplet.java2:    import java.io.*;3:    import java.awt.*;4:    import java.awt.event.*;5:    import java.applet.*;6:    import javax.swing.*;7:    import javax.swing.border.*;8:9:    public class TestApplet extends JApplet      implements ActionListener {10:   private JPanel pane = null;11:     private JScrollPane scrolling = null;12:     private JTextPane fileBox = null;13:     private JTextField tfFilename = null;14:     private JButton butLoad = null;15:     private final String LOAD = "load";16:17:   public void init() {18:   try {19:       jbInit();20:     } catch(Exception e) {21:       e.printStackTrace();22:     }23:   }24:25:   // method which will read data from file, and return it in      // String25:   public String readFile(String fn) {26:     String thisLine, ret = "";27:     try {28:       FileInputStream fin =  new FileInputStream(fn);29:       BufferedReader myInput = new BufferedReader                         (new InputStreamReader(fin));30:       while ((thisLine = myInput.readLine()) != null) {  31:         ret += thisLine + "/n";32:       }33:     } catch (Exception e) {34:       ret = "Cannot load, exception!";35:     }36:     return ret;37:   }39:40:   private void jbInit() throws Exception {41:     pane = new JPanel();42:     pane.setBounds(new Rectangle(0, 0, 500, 325));43:     pane.setLayout(null);44:     pane.setBorder(BorderFactory.createEtchedBorder(                       EtchedBorder.LOWERED));45:     pane.setBackground(new Color(221, 194, 219));46:47:     fileBox = new JTextPane();48:     fileBox.setText("");49:     fileBox.setEditable(false);50:     scrolling = new JScrollPane(fileBox);51:     scrolling.setBounds(new Rectangle(16, 65, 295, 225));52:53:     tfFilename = new JTextField();54:     tfFilename.setText("");55:     tfFilename.setBounds(new Rectangle(16, 23, 206, 29));56:57:     butLoad = new JButton();58:     butLoad.setBounds(new Rectangle(231, 23, 80, 30));59:     butLoad.setText("Load");60:     butLoad.setActionCommand(LOAD);61:     butLoad.addActionListener(this);62:63:     pane.add(scrolling);64:     pane.add(tfFilename);65:     pane.add(butLoad);66:67:     setContentPane(pane);68:   }69:70:   public void actionPerformed(ActionEvent e) {71:     if (e.getActionCommand().equals(LOAD)) {72:         fileBox.setText(readFile(tfFilename.getText()));73:     }74:   }75: }

The steps below are also very simple. We need to compile, create a jar package, and create a simple HTML file to demonstrate the text file and try to use it. Maybe it can load a file without using the signature technology. Let's take a look at the following steps to compile and create a jar package:

Javac testapplet. Java
Jar CVF testapplet. Jar testapplet. Class

The CVF parameter specifies the jar package file name as testapplet. jar and displays detailed output information.

Now, let's use any text editor to createTestapplet.htmlFile.

1: 

Then create a text file in the same text editor, such as C:/demo.txt. In this file, you need to enter some words and sentences. Here, we enter helloworld. Therefore, if it is loaded successfully, you should be able to see the content.

Copy yourTestapplet.htmlAndTestapplet. JarFile to your Tomcat webapp Directory, which stores your web applications. Then browse the HTML file. Here isHTTP: /localhost: 8080/green/testapplet.html

When the applet is loaded, enter the text file path (C:/demo.txt) and click "LOAD. What you see is as follows:

OK. We get an exception message. This is not surprising. Let's try to sign it and see what has changed. Run the toolkey tool in the following ways:

Keytool-genkey-alias testapplet-validity 365

 

(To be continued ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.