从网络驱动器打开文件 [英] Open file from network drive

查看:35
本文介绍了从网络驱动器打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建超链接以从网络驱动器 G 中打开文件:

I'm trying to create hyperlinks to open files from network drive G:

这是我的测试 servlet 的一部分:

@WebServlet(name="fileHandler", urlPatterns={"/fileHandler/*"})
public class FileServlet extends HttpServlet 
{
  private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
  ...
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    String requestedFile = request.getPathInfo();
    ...
    File file = new File("G:/test_dir", URLDecoder.decode(requestedFile, "UTF-8")); // cesta se nacita v kazdem doGet
    String contentType = getServletContext().getMimeType(file.getName());

    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(contentType);
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try 
    {
      input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
      output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
      int length;
      while ((length = input.read(buffer)) > 0) 
      {
        output.write(buffer, 0, length);
      }
    } 
    finally 
    { 
      close(output);
      close(input);
    }
  }
}

我的 HTML 组件:

<a href="fileHandler/test.txt">TEST FILE</a>

网络驱动器在应用服务器上映射为 G:\

Network drive is mapped on application server as G:\

在我的本地主机应用服务器上一切正常.我可以从本地驱动器 C: 甚至同一个网络驱动器 G: 打开文件.

Everything is working fine on my localhost application server. I'm able to open file from local drive C: and even from the same network drive G:.

当我在真实服务器上启动 JSF 应用程序时,我只能从本地驱动器打开文件.不是来自 G:驱动器.

When I start the JSF application on real server, I'm able to open files from local drive only. Not from G: drive.

我尝试过简单的 JAVA 应用程序(以查找 java 实例是否可以访问网络驱动器)并且它适用于(服务器和 dev.PC):

I've tried to simple JAVA application (to find if java instance has an access to network drive) and it works on both (server and dev.PC):

public class Test 
{
  static void main(String[] args) 
  {
    String path = "G:/test_dir/test.txt";
    File file = new File(path);
    System.err.println(file.exists() ? "OK" : "NOK");
  }
} 

我尝试了不同的 URI 方案:

I've tried different URI schemes:

  • G:/test_dir
  • G:\\test_dir

并且以下根本不起作用:

And following doesn't work at all:

  • file://server/g:/test_dir
  • ///server/g:/test_dir
  • \\\\server\\g\\test_dir ---> 事实上,这应该可行
  • file://server/g:/test_dir
  • ///server/g:/test_dir
  • \\\\server\\g\\test_dir ---> in fact, this should work

我的开发 PC 和应用服务器之间应该有什么区别?

Where should be a difference between my develop PC and application server?

解决方案:

我发现 网络驱动器的链接在独立的 Tomcat 中不起作用,但在 Eclipse + Tomcat 中工作,所以我必须使用完整的 URI:

I've found that links to network drive doesn't work in standalone Tomcat, but works in Eclipse + Tomcat, so I have to use complete URI:

  • Case Eclipse + Tomcat:路径 G:/test_dir/test.txt 有效
  • Case Standalone Tomcat:路径 \\\\server\\g\\test_dir\\test.txt 有效

推荐答案

我发现 网络驱动器的链接在独立的 Tomcat 中不起作用,但在 Eclipse + Tomcat 中工作,所以我必须使用完整的 URI:

I've found that links to network drive doesn't work in standalone Tomcat, but works in Eclipse + Tomcat, so I have to use complete URI:

  • Case Eclipse + Tomcat:路径G:/test_dir/test.txt有效
  • Case Standalone Tomcat:路径 \\\\server\\g\\test_dir\\test.txt 有效

这篇关于从网络驱动器打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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