【问题标题】:Azure - WebSites - java.io.FileNotFoundException: image.jpg (Access is denied)Azure - 网站 - java.io.FileNotFoundException:image.jpg(访问被拒绝)
【发布时间】:2015-09-25 10:57:08
【问题描述】:

我遇到了Azure WebSites 的问题。 我正在使用javaWebService 接收图像,我的测试环境中的同一个应用程序不会抛出异常,但是当我将其上传到Azure 时,它就坏了。

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

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

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

我找到了这个 link,但这解释了仅适用于 PHPASP.NET 的解决方案,在 java路径我试过了:

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

没有成功!

有人可以帮助我吗?

【问题讨论】:

    标签: java azure azure-web-app-service


    【解决方案1】:

    对于这个问题,如果您想将文件存储到 Azure 网站文件系统中,请尝试使用此路径:

          //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);
    

    您可以使用相同的路径读取文件。 在 Azure 网站上,如果直接使用路径“image.jpg”,该文件可能会存储到 JVM 启动的 Root 路径中。 Azure 网站可以拒绝此文件路径以确保安全。我认为您可以尝试使用完整路径来访问您的文件。另外,you can check your file system via FTP

    但是,根据我的经验,如果我们想将持久化文件存储在 Azure 网站,Azure Blob storage may be the best choice

    【讨论】:

    • 感谢您的回答 Will Shao 我实际上是在尝试使用 blob 存储,我首先从 http 收到一个编码字符串,然后我这样做: File file = new File(name); byte[] data = new URLCodec().decode(encodedPhoto.getBytes()); FileOutputStream outputStream = new FileOutputStream(file.getName()); outputStream.write(数据);当我将它上传到 blob 时: CloudBlockBlob blob = container.getBlockBlobReference(file.getName()); blob.upload(new FileInputStream(file), file.length());
    • 据我了解,Azure站点中WWWROOT的绝对路径是“D:/home/site/wwwroot”,Java web应用中“new File(”.")”的路径是 "D:/Windows/system32/",我们可以通过 new File(".").getAbsolutePath() 进行快速测试得到结果。因此,请尝试使用 String wr_path = "D:\\home\\site\\wwwroot\\";文件file = new File(wr_path+"imageName.jpg"); FileOutputStream outputStream = new FileOutputStream(file);看看它是否能解决问题。有关 Azure Web 应用的更多信息,您可以参考azure.microsoft.com/en-us/documentation/articles/…
    • 我现在做了一个测试,我尝试实例化 new File("D:\\home\\site\\wwwroot\\image.jpg") 并返回给我一个 java.io .FileNotFoundException: image.jpg(访问被拒绝)再次!
    • 我的 java 正在使用 JSF 和 FacesContext.getCurrentInstance().getExternalContext();正在返回 null,我无法测试 getAbsolutePath ();
    【解决方案2】:

    我的问题解决了!

    我刚刚换了

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

    FileUtils.writeByteArrayToFile(file, data);
    

    在路径:"d:\\home\\site\\wwwroot\\webapps\\image.jpg"

    谢谢大家

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多