1。サーバーに保存します
パスに応じてプロジェクトが配置されているサーバーに保存します。
string imgurl = ""; //画像アドレスtry {// url url url = new url(imgurl); // connection urlconnection con = url.openconnection(); //入力ストリームinputstream is = con.getInputStream(); // 1Kデータバッファリングバイト[] bs = new byte [1024]; //データの長さint lenを読み取ります。 //出力ファイルストリーム出力ストリームos = new fileoutputStream( "c://image.jpg"); // path // whileを開始します(len = is.read(bs))!= -1){os.write(bs、0、len); } //完了、すべてのリンクを閉じますos.close(); is.close(); } catch(malformedurlexception e){e.printstacktrace(); } catch(filenotfoundexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }2。ローカルに保存します
ブラウザのダウンロードとしてローカルに保存します。
文字列imgurl = ""; // urlアドレス文字列filename = imgurl.substring(imgurl.lastindexof( '/') + 1); BufferedInputStream is = null; bufferedOutputStream os = null; try {url url = new url(imgurl); this.getServletResponse()。setContentType( "Application/X-MSDownLoad;"); this.getServletResponse()。setheader( "content-disposition"、 "attachment; 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()); byte [] buff = new byte [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(malformedurlexception e){e.printstacktrace(); } catch(unsupportedencodingexception e){e.printstacktrace(); } catch(ioexception e){e.printstacktrace(); }URLに基づいてクライアントやサーバーに写真をダウンロードする上記の簡単な例は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。