2011-03-17 32 views

回答

1

中的Web上下文中,您可以使用ServletOutputStream。此处资源路径信息作为HTTP上的额外路径信息传递。

final ServletOutputStream out = res.getOutputStream(); 
res.setContentType("application/octet-stream"); 
String file = req.getPathInfo(); 
if (file == null) { 
    out.println("Extra path info was null; should be a resource to view"); 
    return; 
} 

// Convert the resource to a URL 
URL url = getServletContext().getResource(file); 
if (url == null) { 
    out.println("Resource " + file + " not found"); 
    return; 
} 

//Serve the file 
InputStream in = url.openStream(); 
byte[] buf = new byte[4 * 1024]; // 4K buffer 
int bytesRead; 
while ((bytesRead = in.read(buf)) != -1) { 
    out.write(buf, 0, bytesRead); 
} 
+0

这是无限制的文件长度输出吗?根据给定的片段,客户端片段应该是什么呢? – user592704 2011-03-17 17:27:56

+0

无论如何,谢谢。它给了我编码风格的愿景:) – user592704 2011-03-19 06:37:54

相关问题