檔案資料以二進位型態存至資料庫中,載時,將二進位資料送至瀏覽器下載
class須繼承HttpServlet
String encodedFilename = "test.txt"; //下載的檔名
response.addHeader(
"Content-Disposition",
"inline; filename=\"" + encodedFilename + "\"");
try {
FileInputStream is = new FileInputStream(fileName); //fileName=實際檔案位置
byte[] buffer = new byte[4096];
int pos = 0;
while ((pos = is.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, pos);
}
} catch (Exception e) {
log.error("Error on writing content : ", e);
}
全站熱搜
留言列表