Solved

How to read/write a file to client from an applet?

Posted on 1998-06-25
12
325 Views
Last Modified: 2013-12-29
Hello.

I have programmed an applet that reads a file from the clients computer(the applet is accessed trough Explorer 4.0
and the applet is on a webserver), the applet uses a FileInputStream to read the file from the client computer. I know that this is not permitted but i have signed the applet with low so i have full permissions, i used the signcode and cabarc tool from Microsoft. My problem is that i still got the JavaSecurityException(see below). What must i do to make it work. I want the applet to read my file and write it back to the clients computer without the JavaSecurityException.

com.ms.security.SecurityExceptionEx[Host]: cannot access file p.zip
      at com/ms/security/permissions/FileIOPermission.check (FileIOPermission.java)
      at com/ms/security/PolicyEngine.deepCheck (PolicyEngine.java)
      at com/ms/security/PolicyEngine.checkPermission (PolicyEngine.java)
      at com/ms/security/StandardSecurityManager.chk (StandardSecurityManager.java)
      at com/ms/security/StandardSecurityManager.checkRead (StandardSecurityManager.java)
      at java/io/FileInputStream.<init> (FileInputStream.java)
      at ZipApp1.Unpackzip (ZipApp1.java)
      at ZipApp1.init (ZipApp1.java)
      at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
      at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
      at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
      at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
      at com/ms/applet/AppletPanel.run (AppletPanel.java)
      at java/lang/Thread.run (Thread.java)
com.ms.security.SecurityExceptionEx[Host]: cannot access file p.zip
      at com/ms/security/permissions/FileIOPermission.check (FileIOPermission.java)
      at com/ms/security/PolicyEngine.deepCheck (PolicyEngine.java)
      at com/ms/security/PolicyEngine.checkPermission (PolicyEngine.java)
      at com/ms/security/StandardSecurityManager.chk (StandardSecurityManager.java)
      at com/ms/security/StandardSecurityManager.checkRead (StandardSecurityManager.java)
      at java/io/FileInputStream.<init> (FileInputStream.java)
      at ZipApp1.Unpackzip (ZipApp1.java)
      at ZipApp1.init (ZipApp1.java)
      at com/ms/applet/AppletPanel.securedCall0 (AppletPanel.java)
      at com/ms/applet/AppletPanel.securedCall (AppletPanel.java)
      at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
      at com/ms/applet/AppletPanel.processSentEvent (AppletPanel.java)
      at com/ms/applet/AppletPanel.run (AppletPanel.java)
      at java/lang/Thread.run (Thread.java)
Microsoft (R) VM for Java (tm), 4.0 Release 4.79.0.2613
==============================================
?  help
c  clear
f  run finalizers
g  garbage collect
m  memory usage
q  quit
t  thread list
==============================================
 
0
Comment
Question by:pierreandersson
  • 6
  • 5
12 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223758
Java Report Online has a good article describing how to sign applets for both browsers. It is located at
http://www.javareport.com/features/9802/sommers.html

Make sure you've followed all the right steps for signing and using applets in MS Explorer.
0
 

Author Comment

by:pierreandersson
ID: 1223759
Now have read the article, but it wont still work, i don't think that the exception is because of the signing because the certificate window is appearing and i have to submit a yes if i am accepting the applet, can't it be in the code, is there another way to load the file instead of FileInputStream(String filename).
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223760
This is a standard way to do it. There are others like RandomAccessFile, but if the first one fails, the others will probably fail too. So you have to solve the security issue first.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:pierreandersson
ID: 1223761
I send you the way i did the signing, maybe you can se some error in the steps i did it.

1. makecert -sk PierreKey -n "CN=PubTest" PierreCert.cer

2. cert2spc PierreCert.cer PierreCert.spc

3. cabarc n App1.cab App1.class

4. signcode -j javasign.dll -jp medium -spc PierreCert.spc -k PierreKey App1.cab

5. chkjava App1.cab


My applet is on a webserver. I use a explorer 4.0 on a Internet connected computer. The applet tries to load the file from my Internet connected computer.

If you wan't to try the applet go to www.konsult.net/javalab/javazip/zipapp.html


0
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
ID: 1223762
Why don't you sign your CAB file at low security level.

The article at http://www.javareport.com/features/9802/sommers.html explains zones and security levels for MS  Explorer. It also tells you what is permitted to an applet signed with certain security level.  Medium security allows you to use com.ms.io.clientstorage package which will probably give you an access to your file. If you sign it with low security, you will be able to use FileInputStream.

For examples of how to use  com.ms.io.clientstorage for accessing client computer see
http://www.javareport.com/features/9711/sommers.html.
0
 

Author Comment

by:pierreandersson
ID: 1223763
I have tried this to but it won't work, it gives me the same error. It is very strange because it seems that i have dont the right thing when i am signing the cab files, can it be the
rights on the file or something like that?
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223764
Could you provide a piece of code which gives you trouble?
0
 

Author Comment

by:pierreandersson
ID: 1223765
Here is the code i think it is this code that gives me problem FileInputStream in =  new FileInputStream("p.zip"); The program does execute in the dos window as an application.

public void Unpackzip()
      {

try {
      
      
FileInputStream in =  new FileInputStream("p.zip");

                  ZipInputStream zi = new ZipInputStream(in);
             
                  FileOutputStream fo = null;
                  
            
                  
                  ZipEntry ze = new ZipEntry("");
                  
      
              

                  
try {            
                while ((ze = zi.getNextEntry())!=null)
                  {
                  String path = new String();
                  String filename = new String();
                filename=filename+path+ze.getName();
                filename = filename.replace('/',File.separatorChar);
                System.out.println("HELLO: "+filename);
                  File destFile = new File(filename);
                      
                        System.out.println("1  "+filename);
                  if (destFile.exists())
                  {
                  System.out.println("OW Not implemented");
                  // Owerwrite
                  
                  }
                  else
                  {
                        System.out.println("27");
                  String parent = destFile.getParent();
                  
                  if (parent!=null)
                  {
                  File parentFile = new File(parent);
                  if (!parentFile.exists())
                  {
                  System.out.println("Makedir");
                  parentFile.mkdirs();
                  }
                  }
                  
                  System.out.println("Innan fo");
                        fo = new FileOutputStream(filename);
           
      System.out.println(ze.getSize()+"   "+ze.getName());
                        
                  while (x < ze.getSize())
                  {      
                        len = zi.read(bArr,0,256);
            
                fo.write(bArr, 0, len);      
                x = x + len;
               }
               x = 0;
         
                           
                        
                        
                  }

}
System.out.println("2");
 in.close();
// fo.close();      
      } catch (ZipException zipex) { System.out.println("Invalid archive!!!"); }


 
 


                  
                  
                  
            }      catch (IOException ioe) {System.out.println(ioe.toString());}
      
      
      


      
      }

0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223766
Code looks ok. What happens if you sign you applet for low security? What if you turn the browser zone to trusted (with low security)? If the browser security is higher than applet's security it should prompt you for permission which it does, right? Does it say that the applet is signed and allows you to view your signing certificate? If it does not, the problem may be in signing.
0
 

Author Comment

by:pierreandersson
ID: 1223767
Can the securityexception be that i have a testcertificate?
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223768
It shouldn't really. If you have a test certificate it should be obvious for anyone who wants to look at your certificate (and thus may want to deny permission) but the program should not care.
0
 
LVL 1

Expert Comment

by:GregL
ID: 1223769
I am having exactly the same problem, except that I have a VeriSign Class 2 certificate and private key.  I see the certificate in the window, but I get the same exception.  By the way there is a really good FAQ here...

http://www.suitable.com/Doc_CodeSigning.shtml#explorer

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question