“Not allowed to load local resource: file:///C:…jpg” Java EE Tomcat

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

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 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

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

回答1:

sending tag would cause user browser to access image from his filesystem. if you have to store images in folder located in c:\images i 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.

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

Here you can find some info about, how to do it.



回答2:

In Chrome, you are supposed to be able to allow this capability with a runtime flag --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

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



回答3:

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.

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



回答4:

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.



回答5:

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

  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
  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


回答6:

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.



回答7:

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.jpg depending on where your process is running.



回答8:

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

On your server.js or app.js file, add the following:

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.

Now, you can simply do:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!