0

I am having a problem with Azure WebSites. I am using java to receive an image from a WebService, the same app in my test environment doesn't throw the exception, but when I upload it to Azure, it brokes.

File file = new File("image.jpg");

FileOutputStream outputStream = new FileOutputStream(file.getName());

//this happens ==> java.io.FileNotFoundException: image.jpg (Access is denied)

I found this link, but that explains solutions just for PHP an ASP.NET, in java I tryed the paths:

D:\\home\\site\\wwwroot\\app_data\\image.jpg
D:\\Program Files (x86)\\apache-tomcat-7.0.50\\image.jpg

without success!

Someone could help me?

Community
  • 1
  • 1

2 Answers2

0

For this issue, if you'd like to store your file into Azure website file system, please try to use this path :

      //store file 
      private static final String SAVE_DIR = "WebContent\\upload";       //write code into your servlets
      // gets absolute path of the web application
      String appPath = request.getServletContext().getRealPath("");
      // constructs path of the directory to save uploaded file
      String savePath = appPath + File.separator + SAVE_DIR;
      // creates the save directory if it does not exists
      File fileSaveDir = new File(savePath);

and you can read file using same path. On Azure website, if you directly used the path "image.jpg", the file maybe store into the Root path of JVM started. Azure website can deny this file path for security. I think you can try to use the full path to access your file. Also, you can check your file system via FTP.

However, according to my experience, if we want to store the persist file in Azure website, Azure Blob storage may be the best choice.

Community
  • 1
  • 1
Will Shao - MSFT
  • 1,189
  • 7
  • 14
  • Thanks for answer Will Shao I am actually trying to use blob storage, I first receive a encodedString from http then I do this: File file = new File(name); byte[] data = new URLCodec().decode(encodedPhoto.getBytes()); FileOutputStream outputStream = new FileOutputStream(file.getName()); outputStream.write(data); When I upload it to blob: CloudBlockBlob blob = container.getBlockBlobReference(file.getName()); blob.upload(new FileInputStream (file), file.length()); – Rodrigo Sordi Jul 08 '15 at 14:49
  • Per my understanding, the absolute path of WWWROOT in Azure site is "D:/home/site/wwwroot", the path of of "new File(".")" in a Java web app is "D:/Windows/system32/", we can perform a quick test via new File(".").getAbsolutePath() to get the result. Thus, please try to use String wr_path = "D:\\home\\site\\wwwroot\\"; File file = new File(wr_path+"imageName.jpg"); FileOutputStream outputStream = new FileOutputStream(file); to see if it resolves the issue. For more info about Azure web app, you can refer to https://azure.microsoft.com/en-us/documentation/articles/web-sites-java-add-app/ – Ming Xu - MSFT Jul 08 '15 at 15:34
  • I have done a test now, I tryied to instantiate new File("D:\\home\\site\\wwwroot\\image.jpg") and it returns to me a java.io.FileNotFoundException: image.jpg (Access is denied) again! – Rodrigo Sordi Jul 08 '15 at 17:14
  • My java is using JSF and the FacesContext.getCurrentInstance().getExternalContext(); is returning null, i couldn't test the getAbsolutePath (); – Rodrigo Sordi Jul 08 '15 at 17:16
0

I solved my problem!

I just changed

FileOutputStream outputStream = new FileOutputStream(file.getName());
outputStream.write(data);

to

FileUtils.writeByteArrayToFile(file, data);

at the path: "d:\\home\\site\\wwwroot\\webapps\\image.jpg"

Thanks All

Nikita Jerschow
  • 836
  • 2
  • 11
  • 24