使用 Webrick 提供图像
我需要从用户计算机上的文件夹中抓取一些图像,并使用 webrick 服务器将它们显示在网页上。我使用以下代码扫描目录,然后包含网络服务器的路径。我获得了图像的正确路径,但当我尝试访问该页面时,我从 Webrick 收到错误 404。我一直在移动文件并更改权限,但没有成功。我有一种感觉 webrick 正在从 usr 文件夹中埋藏的位置运行,但我不确定如何检查它以及如何正确地为其创建图像路径。任何帮助将不胜感激。
require 'webrick'
include WEBrick
s = HTTPServer.new(Port: 2000)
class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
search_dir = 'users/user/Dropbox/ads_dir'
files = Dir[ search_dir + '/*.jpg']
p @file0 = files[0].to_s
p @file1 = files[1].to_s
p @file2 = files[2].to_s
p @file3 = files[3].to_s
res['Content-Type'] = "text/html"
res.body = %{
<img class="img1" src="#{@file0}"/>
<img class="img2" src="#{@file1}"/>
<img class="img3" src="#{@file2}"/>
<img class="img4" src="#{@file3}"/>
</body>
</html>
}
I need to grab a some images from a folder on a users computer and display them on a webpage using a webrick server. I am using the following code to scan the directory and then include the path to the webserver. I am getting correct paths to the images but I am getting an error 404 from Webrick when I try to access the page. I have been moving files around and changing permissions without any success. I have a feeling webrick is running from a location buried inside the usr folder but I'm not sure how to check this and how to correctly create an image path for it. Any help would be greatly appreciated.
require 'webrick'
include WEBrick
s = HTTPServer.new(Port: 2000)
class HelloServlet < HTTPServlet::AbstractServlet
def do_GET(req, res)
search_dir = 'users/user/Dropbox/ads_dir'
files = Dir[ search_dir + '/*.jpg']
p @file0 = files[0].to_s
p @file1 = files[1].to_s
p @file2 = files[2].to_s
p @file3 = files[3].to_s
res['Content-Type'] = "text/html"
res.body = %{
<img class="img1" src="#{@file0}"/>
<img class="img2" src="#{@file1}"/>
<img class="img3" src="#{@file2}"/>
<img class="img4" src="#{@file3}"/>
</body>
</html>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的图像路径类似于
/home/users/images
您应该声明DocumentRoot
:然后您就可以访问该图像:
If your images path is like
/home/users/images
you should declare theDocumentRoot
:And then you can get access to the image: