2015-10-06 114 views
0

我试图创建超链接,打开从网络驱动器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:\

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

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

我试着简单JAVA应用(找到,如果Java实例可以对网络驱动器的访问)和它的作品都(服务器和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方案:

  • G:/test_dir
  • G:\\test_dir

而且下面没有在所有的工作:

  • file://server/g:/test_dir
  • ///server/g:/test_dir
  • \\\\server\\g\\test_dir --->其实,这应该工作

凡应该是我之间建立一个差异PC和应用程序服务器?

SOLUTION:

我发现links to network drive doesn't work in standalone Tomcat, but works in Eclipse + Tomcat,所以我必须使用完整的URI:

  • 案例的Eclipse + Tomcat的:路径G:/test_dir/test.txt工作
  • 案例独立的Tomcat:路径\\\\server\\g\\test_dir\\test.txt工程
+0

有你尝试了服务器上的简单测试应用程序以及你的开发盒? – NickJ

+0

是的,它同时适用于 – gaffcz

+0

在这种情况下,它可能会检查'URLDecoder.decode(requestedFile,“UTF-8”)'的结果是什么 - 也许这是给你一个不存在的文件名 – NickJ

回答

1

如果你可以调试你的服务器和查看日志,试试这个:

String requestedFile = request.getPathInfo(); 
log.debug('requestedFile='+requestedFile); 
String decodedFilename = URLDecoder.decode(requestedFile, "UTF-8"); 
log.debug('decodedFilename='+decodedFilename); 


File dir = new File("G:/test_dir"); 
log.debug('File g:/test_dir is a dir:'+dir.isDirectory()); 

File file = new File(dir, decodedFilename); 
log.debug('requested file = '+file.getAbsolutePath()); 
log.debug('file exists = '+file.isFile()); 

如果你没有一个日志框架设置,则可以使用System.out.println()代替log.debug(),但是这不推荐生产使用。

这不会解决您的问题,但您将能够看到发生了什么。

+0

谢谢,明天我会试试! – gaffcz

+0

我找到了差异。在我的DEV-PC上,它是在集成到Eclipse的tomcat上启动的。在服务器上它直接部署在Tomcat上。当我尝试将它直接部署在我的DEV-PC上的tomcat上时,它也不起作用。所以这似乎是另一个问题.. – gaffcz