-1

我无法读取另一台具有不同 IP 的机器中的文本文件。下面是我的代码。请看一下..

URL url = 
                    new URL("http://10.128.0.1/d:/kiranshare/testout.txt");


                            br = new BufferedReader(new InputStreamReader(is));
            File file=new File(url.getFile());
               System.out.println(file);
               System.out.println(file.getAbsolutePath());
               System.out.println(file.getName()+file.getParentFile());
               System.out.println("url="+file);
            //   InputStream is = url.openStream(); 
               System.out.println("is"+is);
               ByteArrayOutputStream os = new ByteArrayOutputStream();                  
               System.out.println("os"+os);
               byte[] buf = new byte[4096]; 
               int n;                   
               while ((n = is.read(buf)) >= 0)  
                       os.write(buf, 0, n); 
               os.close(); 
               is.close();                      
               byte[] data = os.toByteArray(); 
       } catch (MalformedURLException e) { 
               e.printStackTrace(); 
       } catch (IOException e) { 
               e.printStackTrace(); 
       } 


Please suggest me where I am doing wrong???

Thanks in Advance
4

2 回答 2

1

您不应使用 HTTP 协议和 URL 类。共享文件夹,直接使用共享文件夹路径使用 File 类读取文件。

例如你可以说

java.io.File myFile = new java.io.File("\\\\10.128.0.1\\kiranshare\\testout.txt");

然后您可以使用 BufferedReader 读取文件。确保您有足够的权限来读取该文件。

于 2012-04-04T06:48:07.140 回答
1

请检查您传递的网址new URL("http://10.128.82.93/d:/kiranshare/testout.txt");

我认为应该是这样的new URL("\\10.128.82.93\kiranshare\testout.txt");

如果文件托管在 Web 服务器上,请先尝试从浏览器打开它,然后查看链接是否正确。

于 2012-04-04T06:44:01.983 回答