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

后端 未结 8 1389
忘了有多久
忘了有多久 2020-11-29 06:36

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)

相关标签:
8条回答
  • 2020-11-29 06:43

    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.

    0 讨论(0)
  • 2020-11-29 06:51

    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:

    <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

    0 讨论(0)
  • 2020-11-29 06:52

    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.

    0 讨论(0)
  • 2020-11-29 06:58

    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

    0 讨论(0)
  • 2020-11-29 07:03

    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.

    0 讨论(0)
  • 2020-11-29 07:05

    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.

    0 讨论(0)
提交回复
热议问题