Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNLP FileSaveService opens a file open dialog

Hi I'm trying to save a file from a Java Webstart Application.

public class Main {
   public static void main(String[] args) {
      try {
         FileSaveService fos = (FileSaveService) ServiceManager.lookup("javax.jnlp.FileSaveService");
         //open Dialog
         FileContents fc = fos.saveFileDialog("c:/data", null, new ByteArrayInputStream("Hallo Welt".getBytes()), "name.txt");
         System.out.println("FileContents: " + fc);
      } catch (UnavailableServiceException e) {
         System.err.println("***" + e + "***");
      } catch (IOException e) {
         System.err.println("***" + e + "***");
      }
      //wait a minute
      try {
         Thread.sleep(10000);
      } catch (InterruptedException e) {
      }
      System.exit(0);
   }
}

Everything works except that the dialog that comes up looks like a "open" file dialog, not like a "save" file dialog:

FileDialog

Any help would be appreciated.

like image 440
keuleJ Avatar asked May 17 '13 12:05

keuleJ


1 Answers

The File-Open-dialog is necessary. You first need to let the user choose where to save the data. Thus a previous call to openFileDialog is absolute necessary for a jnlp-application. You are not allowed to directly save to a specific location like c:

If you follow the mentioned link (http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/examples.html#FileSaveService) you should be successful.

EDIT: for clarification. Saving via javax.jnlp.FileSaveService does exactly need one call. For instance calling saveFileDialog() like this should be sufficient:

fss.saveFileDialog(null, null, new ByteArrayInputStream("Hallo Welt".getBytes()    ), "newFileName.txt");

The necessity of one User-Dialogue is due to the anonymizing nature of jnlp, where your application should not get any hint about the user-filesystem. However, I have to admit, that this was not your question.

Your main trouble comes from the java app everytime presenting the "open-dialogue" instead of the "save-dialogue".

This should not happen! If I may humbly assume from your snippet where you call fos.saveFileDialog: did you just initialize fos by the FileOpenService instead of the FileSaveService?

More details on the FileSaveService can be found here: http://docs.oracle.com/javase/7/docs/jre/api/javaws/jnlp/javax/jnlp/FileSaveService.html

like image 59
Enno Avatar answered Oct 25 '22 04:10

Enno