2011-03-17 60 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

相關問題