serving images using Webrick
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>
}
If your images path is like /home/users/images
you should declare the DocumentRoot
:
s = HTTPServer.new(Port: 2000, DocumentRoot: "/home/users")
And then you can get access to the image:
http://localhost/images/image1.jpg
精彩评论