1. 서버에 저장하십시오
경로에 따라 프로젝트가있는 서버에 저장하십시오.
String imgurl = ""; // 이미지 주소 try {// construct url url url = new url (imgurl); // 연결 열기 urlConnection con = url.openConnection (); // 입력 스트림 입력 스트림은 = con.getInputStream (); // 1K 데이터 버퍼링 바이트 [] bs = 새로운 바이트 [1024]; // 데이터 길이 읽기 int len; // 출력 파일 스트림 스트림 outputStream os = 새 FileOutputStream ( "c : //image.jpg"); // 경로 저장 // while while ((len = is.read (bs))! = -1) {os.write (bs, 0, len); } // 완료, 모든 링크를 닫습니다 os.close (); is.close (); } catch (marlomedurlexception e) {e.printstacktrace (); } catch (filenotfoundException e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); }2. 로컬에 저장하십시오
로컬로 브라우저 다운로드로 저장하십시오.
String imgurl = ""; // url 주소 문자열 filename = imgurl.substring (imgurl.lastindexof ( '/') + 1); bufferedInputStream은 = null; BufferedOutputStream os = null; try {url url = new url (imgurl); this.getServletResponse (). SetContentType ( "Application/X-MSDownload;"); this.getServletResponse (). SetHeader ( "Content-Disposition", "첨부 파일; filename =" + new String (filename.getBytes ( "UTF-8"), "iso8859-1"); this.getServletResponse (). SetHeader ( "Content-Length", String.Valueof (url.openConnection (). getContEntlength ())); is = new bufferedInputStream (url.openstream ()); os = new bufferedoutputStream (this.getServletResponse (). getoutputStream ()); 바이트 [] buff = 새로운 바이트 [2048]; int bytesread; while (-1! = (bytesread = is.read (buff, 0, buff.length))) {os.write (buff, 0, bytesread); } if (is! = null) is.close (); if (os! = null) os.close (); } catch (marlomedurlexception e) {e.printstacktrace (); } catch (UnsupportedEncodingException e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); }URL을 기반으로 클라이언트 및 서버에 사진을 다운로드하는 위의 간단한 예는 내가 공유하는 모든 콘텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.