1. auf dem Server speichern
Speichern Sie es auf dem Server, auf dem sich das Projekt entsprechend dem Pfad befindet.
String imgurl = ""; // Bildadresse try {// URL URL URL = new URL (imgurl); // Verbindungs urlConnection con = url.openconnection (); // Eingabestrom inputStream ist = con.getInputStream (); // 1k Datenpuffer Byte [] BS = neues Byte [1024]; // Datenlänge lesen int len; // Ausgabedatei Stream OutputStream OS = new FileOutputStream ("c: //image.jpg"); // Pfad speichern // Lesen Sie mit (len = is.read (bs))! = -1) {os.write (bs, 0, len); } // Vervollständigen Sie alle Links os.close (); IS.CLOSE (); } catch (fehl Formaledurlexception e) {e.printstacktrace (); } catch (FilenotFoundException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); }2. Vor Ort speichern
Speichern Sie lokal als Browser -Download.
String imgurl = ""; // url address String mateiname = imgurl.substring (imgurl.lastindexof ('/') + 1); BufferedInputStream ist = null; BufferedOutputStream os = null; try {url url = new url (imgurl); this.getServletResponse (). setContentType ("application/x-msdownload;"); this.getServletresponse (). this.getServletResponse (). ist = neuer BufferedInputStream (url.openstream ()); OS = New BufferedOutputStream (this.getServletResponse (). getOutputStream ()); Byte [] Buff = New Byte [2048]; int bytesread; while (-1! } if (ist! = null) ist.close (); if (os! = null) os.close (); } catch (fehl Formaledurlexception e) {e.printstacktrace (); } catch (unportedenCodingException e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); }Das obige einfache Beispiel für das Herunterladen von Bildern auf Clients und Server basierend auf URLs ist der gesamte Inhalt, den ich mit Ihnen teile. Ich hoffe, Sie können Ihnen eine Referenz geben und ich hoffe, Sie können wulin.com mehr unterstützen.