“不允许加载本地资源:file:///C:....jpg”Java EE Tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23969953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:27:32  来源:igfitidea点击:

"Not allowed to load local resource: file:///C:....jpg" Java EE Tomcat

javatomcatstorage

提问by K.Ariche

I'm trying to retrieve a picture from my file system after a good storage,(instead of putting it in the database I copy it to the disc and i put the path to the db)

我正在尝试在良好存储后从我的文件系统中检索图片(而不是将其放入数据库中,而是将其复制到光盘并将路径放入数据库中)

  1. I had store the picture to c:\images\ folder and supposing that the name the complete path is c:\images\mypic.jpg
  2. when I try to retrieve it a set the img src attribute to <img src="c:\images\mypic.jps">by using some java code
  3. in the browser console I found this error Not allowed to load local resource: file:///C://images//mypic.jpg
  1. 我已将图片存储到 c:\images\ 文件夹并假设完整路径的名称为 c:\images\mypic.jpg
  2. 当我尝试<img src="c:\images\mypic.jps">使用一些 Java 代码检索它时,将 img src 属性设置为
  3. 在浏览器控制台中,我发现了这个错误 Not allowed to load local resource: file:///C://images//mypic.jpg

Question:how to fix these path problem ? where Should I store the pictures ? and from where should I retrieve them ?

问题:如何解决这些路径问题?我应该在哪里存储图片?我应该从哪里取回它们?

采纳答案by T.G

sending tag <img src="c:\images\mypic.jpg">would cause user browser to access image from his filesystem. if you have to store images in folder located in c:\imagesi would suggest to create an servlet like images.jsp, that as a parameter takes name of a file, then sets servlet response content to an image/jpg and then loads bytes of image from server location and put it to a response.

发送标签<img src="c:\images\mypic.jpg">会导致用户浏览器从他的文件系统访问图像。如果您必须将图像存储在位于的文件夹中,c:\images我建议创建一个像images.jsp这样的servlet,它作为参数采用文件名,然后将servlet响应内容设置为图像/jpg,然后从服务器加载图像字节位置并将其放入响应中。

But what you use to create your application? is it pure servlet? Spring? JSF?

但是你用什么来创建你的应用程序?它是纯servlet吗?春天?JSF?

Hereyou can find some info about, how to do it.

在这里你可以找到一些关于如何做的信息。

回答by Mifmif

You have Two alternatives :

您有两种选择:

First one is to create a ServletImageLoader that would take as a parameter an identifier of your image (the path of the image or a hash) that you will use inside the Servlet to handle your image, and it will print to the response stream the loaded image from the server.

第一个是创建一个 ServletImageLoader,它将图像的标识符(图像的路径或哈希)作为参数,您将在 Servlet 中使用该标识符来处理您的图像,并且它将打印到响应流中加载来自服务器的图像。

Second one is to create a folder inside your application's ROOT folder and just save the relative path to your images.

第二个是在应用程序的 ROOT 文件夹中创建一个文件夹,然后保存图像的相对路径。

回答by cherouvim

The concept of http location and disk location is different. What you need to do is:

http位置和磁盘位置的概念是不同的。你需要做的是:

  1. for uploaded file summer.jpg
  2. move that under a known (to the application) location to disk, e.g c:\images\summer.jpg
  3. insert into db record representing the image with text summer.jpg
  4. to display it use plain <img src="images/summer.jpg" />
  5. you need something (e.g apache) that will serve c:\images\under your application's /images. If you cannot do this then in step #2 you need to save somewhere under your web root, e.g c:\my-applications\demo-app\build\images
  1. 对于上传的文件 summer.jpg
  2. 将其在已知(到应用程序)位置下移动到磁盘,例如 c:\images\summer.jpg
  3. 插入表示带有文本的图像的数据库记录 summer.jpg
  4. 显示它使用普通 <img src="images/summer.jpg" />
  5. 您需要一些(例如 apache)可以c:\images\在您的应用程序的/images. 如果你不能这样做,那么在第 2 步你需要保存在你的 web 根目录下的某个地方,例如c:\my-applications\demo-app\build\images

回答by Brett Okken

Many browsers have changed their security policies to no longer allow reading data directly from file shares or even local resources. You need to either place the files somewhere that your tomcat instance can serve them up and put a "regular" http url in the html you generate. This can be accomplished by either providing a servlet which reads and provides the file putting the file into a directory where tomcat will serve it up as "static" content.

许多浏览器已更改其安全策略,不再允许直接从文件共享甚至本地资源读取数据。您需要将文件放置在您的 tomcat 实例可以提供它们的地方,并在您生成的 html 中放置一个“常规”http url。这可以通过提供一个 servlet 来完成,该 servlet 读取并提供文件,将文件放入目录中,tomcat 将在该目录中将其作为“静态”内容提供。

回答by Roger

In Chrome, you are supposed to be able to allow this capability with a runtime flag --allow-file-access-from-files

在 Chrome 中,您应该能够使用运行时标志 --allow-file-access-from-files 来允许此功能

However, it looks like there is a problem with current versions of Chrome (37, 38) where this doesn't work unless you also pass the runtime flag --disable-web-security

但是,当前版本的 Chrome (37, 38) 似乎存在问题,除非您还传递运行时标志 --disable-web-security,否则这不起作用

That's an unacceptable solution, except perhaps as a short-term workaround, but it has been identified as an issue: https://code.google.com/p/chromium/issues/detail?id=379206

这是一个不可接受的解决方案,除了作为短期解决方法,但它已被确定为一个问题:https: //code.google.com/p/chromium/issues/detail?id =379206

回答by Harun ERGUL

This error means you can not directly load data from file system because there are security issues behind this. The only solution that I know is create a web service to serve load files.

此错误意味着您无法直接从文件系统加载数据,因为这背后存在安全问题。我知道的唯一解决方案是创建一个 Web 服务来提供加载文件。

回答by user6147214

Do not use ABSOLUTE PATH to refer to the name of the image for example: C:/xamp/www/Archivos/images/templatemo_image_02_opt_20160401-1244.jpg. You must use the reference to its location within webserver. For example using ../../Archivos/images/templatemo_image_02_opt_20160401-1244.jpgdepending on where your process is running.

不要使用绝对路径来引用图像的名称,例如:C:/xamp/www/Archivos/images/templatemo_image_02_opt_20160401-1244.jpg。您必须使用对其在网络服务器中的位置的引用。例如,../../Archivos/images/templatemo_image_02_opt_20160401-1244.jpg根据您的进程运行的位置使用。

回答by Pedro Hoehl Carvalho

Here is a simple expressjssolution if you just want to run this app locally and security is not a concern:

expressjs如果您只想在本地运行此应用程序并且不担心安全性,这里有一个简单的解决方案:

On your server.jsor app.jsfile, add the following:

在您的server.jsapp.js文件中,添加以下内容:

app.use('/local-files', express.static('/'));

That will serve your ENTIRE root directory under /local-files. Needless to say this is a really bad idea if you're planning to deploy this app anywhere other than your local machine.

这将为您的整个根目录提供/local-files. 毋庸置疑,如果您计划将此应用程序部署到本地计算机以外的任何地方,那么这是一个非常糟糕的主意。

Now, you can simply do:

现在,您可以简单地执行以下操作:

<img src="/local-files/images/mypic.jps"/> 

note: I'm running macOS. If you're using Windows you may have to search and remove 'C:\' from the path string

注意:我正在运行 macOS。如果您使用的是 Windows,则可能需要从路径字符串中搜索并删除 'C:\'