Os exemplos deste artigo compartilham o código específico para o download do arquivo de implementação Java para sua referência. O conteúdo específico é o seguinte
Public HttpServletResponse Download (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 (); } resposta de retorno; } public void DownloadLocal (httpServletResponse resposta) lança fileNotfoundException {// download de file string local filename = "operator.doc" .toString (); // O nome padrão salvar o arquivo // leia inputStream Instrateam = new FileInputStream ("c: /operator.doc"); // o caminho de armazenamento de arquivos // Defina o formato da resposta de saída.reset (); Response.setContentType ("bin"); Response.addHeader ("Content-Disposition", "Anexamento; nome do arquivo =/" " + nome do arquivo +"/""); // Faz os dados no byte de fluxo [] b = novo byte [100]; int len; tente {while ((len = Instream.read (b))> 0) Response.getOutputStream (). Write (b, 0, len); Instruam.close (); } catch (ioexception e) {e.printStackTrace (); }} public void DownloadNet (httpServletResponse Response) lança malformedUrlexception {// download the Network File int byTesum = 0; int byteread = 0; Url url = new url ("windine.blogdriver.com/logo.gif"); tente {urlConnection Conn = url.openconnection (); InputStream unstream = conn.getInputStream (); FileOutputStream fs = new FileOutputStream ("c: /abc.gif"); byte [] buffer = novo byte [1204]; comprimento int; while ((byteread = Instream.read (buffer))! = -1) {bytesum += byteread; System.out.println (bytesum); fs.write (buffer, 0, byteread); }} catch (filenotfoundException e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); }}// Uma maneira de abrir arquivos online
download público void (string filepath, httpServletResponse resposta, boolean isonline) lança a exceção {arquivo f = novo arquivo (filepath); if (! f.Exists ()) {Response.SenderRor (404, "Arquivo não encontrado!"); retornar; } BufferInputStream Br = new BufferInputStream (new FileInputStream (f)); byte [] buf = novo byte [1024]; int len = 0; Response.Reset (); // muito importante se (isonline) {// online opear método url u = new url ("arquivo: ////" + filepath); Response.setContentType (U.OPenconnection (). GetContentType ()); Response.setheader ("Content-Disposition", "Inline; filename =" + f.getName ()); // O nome do arquivo deve ser codificado como UTF-8} else {// Método de download puro Response.SetContentType ("Application/X-MsDownload"); Response.setheader ("Content-Disposition", "Applement; filename =" + f.getName ()); } OutputStream out = Response.getOutputStream (); while ((len = Br.read (buf))> 0) out.write (buf, 0, len); br.close (); out.Close (); }O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.