开发者

Locating a file in a network disk in a Servlet

I create ImageServlet to refer to videos out of my web application scope.

The location of all of my videos are on a intranet location that could be reached from any computer in the intranet:

String path = "\\myip\storage\ogg\VX-276.ogg"

In my application, when I write it as URL - it can't display it!

If I try to open it with chrome it automatically changes it to file://myip/storage/ogg/VX-276.ogg and the file is being displayed.

I tried to do so: file:////odelyay_test64/storage/ogg/ as well but Java converts the string to: file:\myip\storage\ogg\VX-276.ogg which does not exist!

What is the correct way to refer to it?

EDITED

I create a small test:

String path = "file://myip/storage/ogg/VX-276.ogg";
        File file = n开发者_如何学Pythonew File(path);
        if (file.exists())
            System.out.println("exists");
        else {
            System.out.println("missing" + file.getPath());
        }

and I get:

missing file:\myip\storage\ogg\VX-276.ogg

As you can see the slashes are being switched


As per your previous question, you're referencing the resource in a HTML <video> tag. All URLs in the HTML source code must be http:// URLs (or at least be relative to a http:// URL). Most browsers namely refuse to load resources from file:// URLs when the HTML page is itself been requested by http://. You just need to let the URL point to the servlet. If the servlet's doGet() method get hit, then the URL is fine and you should not change it.

Your concrete problem is in the way how you open and read the desired file in the servlet. You need to ensure that the path in File file = new File(path) points to a valid location before you open a FileInputStream on it.

String path = "file://myip/storage/ogg/VX-276.ogg";
File file = new File(path);
// ...

If the servlet code is well written that it doesn't suppress/swallow exceptions and you have read the server logs, then you should have seen an IOException such as FileNotFoundException along with a self-explaining message in the server logs whenever reading the file fails. Go read the server logs.


Update as per the comments, it turns out that you're using Windows and thus file:// on a network disk isn't going to work for Java without mapping it on a drive letter. You need to map //myip on a drive letter first, for example X:.

String path = "X:/storage/ogg/VX-276.ogg";
File file = new File(path);
// ...


in the end I used VFS library of apache and my code looks like this:

public static void main(String[] args) {
        FileSystemManager fsManager = null;
        String path = "\\\\myip\\storage\\ogg\\VX-276.ogg";
        try {
             fsManager = VFS.getManager();

        FileObject basePath;


            basePath = fsManager.resolveFile("file:" + path);




            if (basePath.exists())
                System.out.println("exists");
            else {
                System.out.println("missing" + basePath.getURL());
            }
        } catch (FileSystemException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

In this way, I don't need to create a driver for each user of the system and it allows me not to depend on operation system!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜