简体   繁体   English

不允许加载本地资源:JSP

[英]Not allowed to load local resource: JSP

I want to download a simple file by a link in jsp, but this name have a Chinese characters. 我想通过jsp中的链接下载一个简单的文件,但是这个名称有一个汉字。 Here my simple code : 这是我的简单代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!doctype html>
<html lang="fr">
  <head>
   <meta charset="utf-8">
   <title>Titre de la page</title>
  </head>
 <body>
     <a href="C:\Files\4.导轨安装板.PDF" target="_blank">My link </a>
 </body>
 </html>

that gives an error in navigator console : Not allowed to load local resource. 在导航器控制台中出现错误:不允许加载本地资源。

Thank you for your help :) 谢谢您的帮助 :)

The problem are not the characters is the fact that you are trying to access the local disk. 问题不是字符,而是您正在尝试访问本地磁盘的事实。 Your code is executing in a browser, and, for security reasons, you cannot access the local disk of the machine on which the page is displayed. 您的代码正在浏览器中执行,并且出于安全原因,您无法访问显示该页面的计算机的本地磁盘。 Not to mention that, if that would have been allowed, it will load something different on every machine it runs on :). 更不用说,如果允许这样做,它将在它运行的每台计算机上加载不同的东西:)。 So, either you use relative path in your HREF, or full URLs. 因此,您可以在HREF中使用相对路径,也可以使用完整的URL。 Remember, when you specify a link it goes to the SERVER to get the resource, not from local machine on which the browser is running. 请记住,当您指定链接时,它会去SERVER获取资源,而不是从运行浏览器的本地计算机获取资源。

Do not import or load local resource in this format: 不要以这种格式导入或加载本地资源:

src="file://home/web-server/foo/bar.jpg"

Because modern browsers not allowed to load files on server disk. 因为现代浏览器不允许在服务器磁盘上加载文件。

I suggest that you tell your proxy server to server the dir 我建议您告诉代理服务器将目录

foo

as a static file dir which browsers can access via your proxy server. 作为静态文件目录,浏览器可以通过您的代理服务器访问该目录。 In NodeJS, it will be like (suggest this file in /home/web-server named app.js) 在NodeJS中,就像(建议在/ home / web-server中名为app.js的文件)

var express = require("express");
var app = express();
app.use(express.static(path.join(__dirname, 'foo')));

Now, in your html code, you can call 现在,在您的html代码中,您可以调用

<a href="4.导轨安装板.PDF" target="_blank">My link </a>

Then, you will see browser access your file by 然后,您将看到浏览器通过以下方式访问文件

http://yourdomain.com/4.导轨安装板.PDF

which will work. 这将工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM