Este artigo descreve o método simples de servlet para implementar o download do arquivo. Compartilhe -o para sua referência, como segue:
Download public static void (String Path, HTTPServletResponse Response) {try {// Path refere -se ao caminho do arquivo a ser baixado. Arquivo de arquivo = novo arquivo (caminho); // Obtenha o nome do arquivo. String filename = file.getName (); // Obtenha o nome do sufixo do arquivo. String ext = filename.substring (filename.lastIndexOf (".") + 1) .toupPercase (); // Faça o download do arquivo na forma de um fluxo. InputStream fis = new bufferInputStream (new FileInputStream (caminho)); byte [] buffer = novo byte [fis.Available ()]; fis.read (buffer); fis.close (); // resposta clara da resposta da resposta.Reset (); // Defina a resposta do cabeçalho da resposta. Response.addHeader ("Length-comprimento", "" + file.length ()); OutputStream toClient = new BufferoudOutputStream (Response.getOutputStream ()); Response.setContentType ("Application/Octet-Stream"); ToClient.Write (buffer); toClient.flush (); toClient.close (); } catch (ioexception ex) {ex.printStackTrace (); }}Espero que este artigo seja útil para a programação Java de todos.