Permission Denied - Trying to save file with Business Logic

Hi,

I am trying to implement a business logic that takes an uploaded file’s URL from the data table saves back a manipulated form of the same file. However, I’m getting a permission denied error. I have gone through the forums and I even added a user login so that my code runs as authenticated user on the server. it still does not seem to solve the problem.

the error im getting is:







p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {font-variant-ligatures: no-common-ligatures}



java.security.AccessControlException: access denied ("java.io.FilePermission" "myhelloworld-async.txt" "write")
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
	at java.security.AccessController.checkPermission(AccessController.java:884)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
	at com.backendless.coderunner.runtime.security.CodeRunnerSecurityManager.checkPermission(CodeRunnerSecurityManager.java:58)
	at java.lang.SecurityManager.checkWrite(SecurityManager.java:979)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:200)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
	at com.printerapplication.events.persistence_service.Prints_ReceivedTableEventHandler.uploadAsync(Unknown Source)
	at com.printerapplication.events.persistence_service.Prints_ReceivedTableEventHandler.beforeCreate(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.backendless.coderunner.runtime.task.EventInvocationTask.runImpl(EventInvocationTask.java:113)
	at com.backendless.coderunner.runtime.concurrent.ExtendedRunnable.run(ExtendedRunnable.java:26)






p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
span.s1 {font-variant-ligatures: no-common-ligatures}
span.Apple-tab-span {white-space:pre}




















	at java.lang.Thread.run(Thread.java:748)

the code I am trying to run is from this link from backendless.


private static void uploadAsync() throws Exception
{
  // create a file locally so there is something to upload
  String filename = "myhelloworld-async.txt";
  FileOutputStream fileOutputStream = new FileOutputStream( filename );
  fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
  fileOutputStream.close();
  final File file = new File( filename );


  // now upload the file
  Backendless.Files.upload( file, "/myfiles", new AsyncCallback<BackendlessFile>()
  {
    @Override
    public void handleResponse( BackendlessFile uploadedFile )
    {
      System.out.println( "File has been uploaded. File URL is - " + uploadedFile.getFileURL() );
      file.delete();
    }


    @Override
    public void handleFault( BackendlessFault backendlessFault )
    {
      System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
    }
  } );
}

Any help would be highly appreciated.

Thanks in Advance!

Hi Usama,

The problem is right here:

String filename = "myhelloworld-async.txt";
FileOutputStream fileOutputStream = new FileOutputStream( filename );
fileOutputStream.write( "Hello mbaas!\nUploading files is easy!".getBytes() );
fileOutputStream.close();

You cannot write to the local file system from business logic. All File IO must be done with the API we provide.

Regards,
Mark