Java Applet Download File

前端 未结 2 1131
有刺的猬
有刺的猬 2021-01-06 18:07

I am trying to build a java applet which downloads a file to the client machine. As a java application this code worked fine but when I tried as an applet it does nothing. I

相关标签:
2条回答
  • 2021-01-06 18:43
    FileOutputStream fos = new FileOutputStream("google.html");
    

    Change that to:

    File userHome = new File(System.getProperty("user.home"));
    File pageOutput = new File(userHome, "google.html");
    FileOutputStream fos = new FileOutputStream(pageOutput);  //see alternative below
    

    Ideally you would put the output in either of:

    1. A sub-directory (perhaps based on the package name of the main class - to avoid collisions) of user.home. user.home is a place where the user is supposed to be able to read & create files.
    2. A path as specified by the end user with the help of a JFileChooser.
    0 讨论(0)
  • 2021-01-06 18:49

    See this question,

    Self Signed Applet Can it access Local File Systems

    I believe it will help you, you need to write your code to use PrivilegedAction.

    http://docs.oracle.com/javase/1.4.2/docs/api/java/security/PrivilegedAction.html

    0 讨论(0)
提交回复
热议问题