通过网络路径读取文件 [英] Reading a file over a network path

查看:100
本文介绍了通过网络路径读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个奇怪的问题,当我使用

  File FileToRead = new File(\\\\\ \\MYSERVER\\MYFOLDER\\MYFOLDER\\MYPICTURE.JPG); 

通过网络读取文件,我得到的只是一个空指针异常。通常情况下,本地路径与此一起工作,但是当在网络路径上时,我无法设法使其工作。任何想法?

PS:哦,我的网络连接似乎工作,访问Windows资源管理器中的数据时没有问题...

更多代码:

 文件FileToRead =新文件(file:// DOKSERVICE / Somefolder / ProductImage /01001.JPG); 
// File FileToRead = new File(c:\\dog.jpg);本地测试
BufferedImage image = ImageIO.read(FileToRead);
BufferedImage resizedimage = new BufferedImage(260,260,BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedimage.createGraphics();
g.drawImage(image,0,0,260,260,null);
g.dispose();
picture.setIcon(new ImageIcon(image));


解决方案

只需指定文件路径为URI:

  File FileToRead = new File(new URI(file://MYSERVER/MYFOLDER/MYFOLDER/MYPICTURE.JPG)); 

编辑注意那个字符串是一个URI!它不能包含空格,所以你必须用%20替换它们才能工作。


I have this weird issue, when I use

File FileToRead = new File("\\\\MYSERVER\\MYFOLDER\\MYFOLDER\\MYPICTURE.JPG");

to read a file over a network, all I get is a null pointer exception. Normally a local path works with this, but when on a network path, I just couldn't manage to get it to work. Any ideas?

PS: oh and my network connection seems to work, no issues when accessing data in windows explorer...

More of the code:

File FileToRead = new File("file://DOKSERVICE/Somefolder/ProductImage/01001.JPG");
//File FileToRead = new File("c:\\dog.jpg"); local test
BufferedImage image = ImageIO.read(FileToRead);
BufferedImage resizedimage = new BufferedImage(260, 260,BufferedImage.TYPE_INT_RGB ); 
Graphics2D g = resizedimage.createGraphics();  
g.drawImage(image, 0, 0, 260, 260, null);  
g.dispose(); 
picture.setIcon(new ImageIcon(image));

解决方案

Just specify the file path as URI:

File FileToRead = new File(new URI("file://MYSERVER/MYFOLDER/MYFOLDER/MYPICTURE.JPG"));

EDIT note that that string is an URI! It cannot contain spaces so you have to replace them with "%20" to make it work.

这篇关于通过网络路径读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆