Solved

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

Posted on 1998-06-25
12
320 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:pierreandersson
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
Could you provide a piece of code which gives you trouble?
0
 

Author Comment

by:pierreandersson
Comment Utility
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
Comment Utility
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
Comment Utility
Can the securityexception be that i have a testcertificate?
0
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now