Varias formas de descargar archivos Java, como sigue:
Descargar public httpServletResponse (ruta de cadena, respuesta httpservletResponse) {try {// ruta se refiere a la ruta del archivo que se descargará. Archivo archivo = nuevo archivo (ruta); // Obtenga el nombre del archivo. Cadena filename = file.getName (); // Obtenga el nombre de sufijo del archivo. Cadena ext = filename.substring (FileName.lastIndexof (".") + 1) .ToUpperCase (); // Descargue el archivo en forma de transmisión. InputStream FIS = new BufferedInputStream (nuevo FileInputStream (ruta)); byte [] buffer = new byte [fis.available ()]; fis.read (búfer); fis.close (); // Respuesta de respuesta clara Respuesta.reset (); // Establecer la respuesta del encabezado de la respuesta Respuesta.AddHeader ("Content-Length", "" + file.length ()); OutputStream TOCLENT = new BufferedOutputStream (Response.getOutputStream ()); respuesta.setContentType ("Application/Octet-stream"); TOclient.write (buffer); tOclient.flush (); tOclient.close (); } Catch (ioException ex) {ex.printstacktrace (); } Respuesta de retorno; } public void downloadLocal (httpservletResponse respuesta) lanza FileNotFoundException {// Descargar el archivo local String FileName = "Operator.doc" .ToString (); // El nombre de guardado predeterminado del archivo // Leer InputStream InSteam = new FileInputStream ("c:/operator.doc"); // La ruta de almacenamiento del archivo // Establece el formato de la salida respuesta.reset (); respuesta.setContentType ("bin"); Respuesta.AddHeader ("Disposición de contenido", "Adjunto; FileName =/" " + FileName +"/""); // bucle los datos en el byte de flujo [] b = nuevo byte [100]; int len; Pruebe {while ((len = Instail.read (b))> 0) respuesta.getOutputStream (). Write (B, 0, Len); Intileam.close (); } catch (ioException e) {E.PrintStackTrace (); }} public void downloadnet (httpservletResponse respuesta) lanza malformedUreLException {// Descargar el archivo de red int bytesum = 0; int byteread = 0; Url url = nueva url ("windine.blogdriver.com/logo.gif"); intente {urlconnection conn = url.openconnection (); InputStream InSteam = Conn.getInputStream (); FileOutputStream fs = new FileOutputStream ("c: /abc.gif"); byte [] buffer = new Byte [1204]; int longitud; while ((byteread = instramp.read (buffer))! = -1) {bytesum += byteread; System.out.println (bytesum); fs.write (buffer, 0, byteread); }} Catch (FileNotFoundException e) {E.PrintStackTrace (); } catch (ioException e) {E.PrintStackTrace (); }}// Una forma de abrir archivos en línea
Descargar public void (String FilePath, HttpServletResponse Respuesta, boolean isonline) lanza la excepción {archivo f = nuevo archivo (filePath); if (! f.exists ()) {Response.sendError (404, "¡Archivo no encontrado!"); devolver; } BufferedInputStream BR = new BufferedInputStream (nuevo FileInputStream (f)); byte [] buf = nuevo byte [1024]; int len = 0; respuesta.reset (); // muy importante if (isonline) {// en línea abre método url u = new url ("archivo: ////" + filepath); respuesta.setContentType (U.OpenConnection (). GetContentType ()); respuesta.setheader ("Disposición de contenido", "inline; filename =" + f.getName ()); // El nombre del archivo debe codificarse como UTF-8} else {// Pure Download Method Response.SetContentType ("Aplicación/X-MSDload"); respuesta.setheader ("Disposición de contenido", "Adjunto; FileName =" + F.getName ()); } OutputStream out = Response.getOutputStream (); while ((len = br.read (buf))> 0) out.write (buf, 0, len); br.close (); out.close (); }Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.