A estrutura do projeto WebApp é a seguinte:
O conteúdo do arquivo Download.html são os seguintes:
<! Doctype html> <html> <head> <meta charset = "utf-8"> <title> Insira o título aqui </title> </head> <body> <h1> download de recursos: </h1> <p> Ao usar a etiqueta, apenas os arquivos que não podem ser parsados pelo brawer serão baixados. </p> <a href = "/web/resource/a.mp3" rel = "Nofollow externo"> a.mp3 </a> <br> <a href = "/web/resource/a.exe" rel = "Nofollow" a.exe </a> <br> <b href = "/web/recursos/a.ttt"> href = "/web/resource/a.xlsx" rel = "Nofollow externo"> a.xlsx </a> <br> <a href = "/web/resource/a.png" rel = "Nofollow externo"> a.png </a> <br> <p> SO, usando a tag combinada com o servlet "> href = "/web/download? nome do arquivo = a.mp3" rel = "nofollow externo"> a.mp3 </a> <br> <a href = "/web/download? nome do arquivo = a.exe" rel = "nofollow"> a.exe </a> <br> <a href = "/web/download? href = "/web/download? nome do arquivo = a.xlsx" rel = "externo nofollow"> a.xlsx </a> <br> <a href = "/web/download? nome do arquivo = a.png" rel = "nofollow"> a.png </a> <br> </body> </html>
O conteúdo do arquivo servlet download.java download é o seguinte:
pacote com.download.servlet; importar java.io.fileInputStream; importar java.io.ioException; importar java.io.inputStream; importar javax.servlet.sertletexception; importação javax.servlet.servlettletTream; importtream; javax.servlet.http.httpServletRequest; importar javax.servlet.http.httpServletRequest; importar javax.servlet.http.httpServletSponse;/** * Servlet da classe Long */Public Class Ocorre HttServlet {private; Void protegido Doget (solicitação httpServletRequest, httpServletResponse Response) lança servletexception, ioexception {// 1. Obtenha o nome do arquivo solicitado para baixar string filename = request.getParameter ("nome do arquivo"); // 2. Obtenha o caminho do sistema de arquivos do arquivo string filepath = request.getServletContext (). GetRealPath ("Resource/"+FileName); // 3. Defina o cabeçalho da resposta para solicitar o navegador a não analisar os dados do arquivo de resposta, mas para analisá -los na forma de anexo, ou seja, a função de download Response.setContentType (this.getServletContext (). GetMimetype (FileName)); Response.setheader ("Content-Disposition", "Applement; FileName ="+FileName); // 4. Leia o fluxo de entrada do arquivo e o fluxo de saída de resposta e produza os dados para o cliente inputStream in = new FileInputStream (FilePath); ServletOutputStream Out = Response.getOutputStream (); int len = 0; byte [] buf = novo byte [1024]; while ((len = in.read (buf))! =-1) {out.write (buf, 0, len); } in.close (); } Void protegido doPost (solicitação httpServletRequest, resposta httpServletResponse) lança servletexception, ioexception {doget (solicitação, resposta); }}Digite http: // localhost: 8080/DownloadServlet/Download.html na barra de endereço do navegador.
O exemplo acima explicações da função de download de arquivo fornecida pela Java Web Response é todo o conteúdo compartilhado pelo editor. Espero que possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.