![](/img/trans.png)
[英]How to download and read a .txt file from http URL using my servlet
[英]Download a txt file using a servlet
我正在嘗試制作一個 html 按鈕,它使用servlet將文件從服務器下載到用戶的機器中。 這是代碼:
String id_archivo = (request.getParameter("idArchivo") == null) ? "" : request.getParameter("idArchivo");
String nombre_archivo = (request.getParameter("nomArchivo") == null) ? "" : request.getParameter("nomArchivo");
//Para que se baje el archivo
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment;filename=\"" + nombre_archivo + "\"");
log.info("Buscando archivo nombre[" + nombre_archivo + "] id[" + id_archivo + "]");
byte[] archivo =getFile(id_archivo, nombre_archivo);
response.setContentLength(archivo.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(archivo);
sos.flush();
sos.close();
問題是它不下載文件,它只是在瀏覽器中顯示它。
您需要查看Content-Disposition
標題。 它在這里解釋: http : //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1 - 本質上它允許您指定瀏覽器的“提示”以將文件作為下載處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.