Azure-WebSites-java.io.FileNotFoundException:image.jpg(访问被拒绝)
我在Azure网站上遇到问题。 我使用Azure-WebSites-java.io.FileNotFoundException:image.jpg(访问被拒绝),java,azure,azure-web-app-service,Java,Azure,Azure Web App Service,我在Azure网站上遇到问题。 我使用java从WebService接收图像,测试环境中的同一个应用程序不会引发异常,但当我将其上载到Azure时,它会中断 File file = new File("image.jpg"); FileOutputStream outputStream = new FileOutputStream(file.getName()); //this happens ==> java.io.FileNotFoundException: image.jpg (
java
从WebService
接收图像,测试环境中的同一个应用程序不会引发异常,但当我将其上载到Azure
时,它会中断
File file = new File("image.jpg");
FileOutputStream outputStream = new FileOutputStream(file.getName());
//this happens ==> java.io.FileNotFoundException: image.jpg (Access is denied)
我发现了这一点,但这解释了仅针对PHP
和ASP.NET
的解决方案
java
我尝试了以下路径:
D:\\home\\site\\wwwroot\\app_data\\image.jpg
D:\\Program Files (x86)\\apache-tomcat-7.0.50\\image.jpg
没有成功
有人可以帮我吗?对于此问题,如果您想将文件存储到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启动的根路径中。Azure网站可以出于安全考虑拒绝此文件路径。我认为您可以尝试使用完整路径来访问您的文件。也
然而,根据我的经验,如果我们想在Azure网站中存储持久化文件 我解决了我的问题
我刚换了衣服
FileOutputStream outputStream = new FileOutputStream(file.getName());
outputStream.write(data);
到
路径:“d:\\home\\site\\wwwroot\\webapps\\image.jpg”
谢谢大家谢谢你的回答Will Shao我实际上在尝试使用blob存储,我首先从http收到一个编码字符串,然后我这样做:File File=new File(name);byte[]data=new URLCodec().decode(encodedPhoto.getBytes());FileOutputStream outputStream=新的FileOutputStream(file.getName());outputStream.write(数据);当我上传到blob:CloudBlockBlob blob=container.getBlockBlobReference(file.getName())时;upload(新的FileInputStream(file),file.length());据我所知,Azure站点中WWWROOT的绝对路径是“D:/home/site/WWWROOT”,Java web应用程序中“new File(.”)的路径是“D:/Windows/system32/”,我们可以通过new File(.”.getAbsolutePath()执行快速测试以获得结果。因此,请尝试使用字符串wr_path=“D:\\home\\site\\wwwroot\\”;File File=新文件(wr_path+“imageName.jpg”);FileOutputStream outputStream=新的FileOutputStream(文件);看看它是否解决了这个问题。有关Azure web app的更多信息,请参阅我现在已经做了一个测试,我尝试实例化新文件(“D:\\home\\site\\wwwroot\\image.jpg”),它再次向我返回java.io.FileNotFoundException:image.jpg(访问被拒绝)!我的java正在使用JSF和FacesContext.getCurrentInstance().getExternalContext();返回null,我无法测试getAbsolutePath();
FileUtils.writeByteArrayToFile(file, data);