场景一:
package com. guonian. miaosha ; import java. io. BufferedReader ;
import java. io. File ;
import java. io. IOException ;
import java. io. InputStreamReader ;
import java. net. HttpURLConnection ;
import java. net. MalformedURLException ;
import java. net. URL ; public class Test { public static void main ( String [ ] args) throws IOException { URL url = new URL ( "http://192.168.1.220:8080/file/%FL%AC%94%E8%BA%.txt" ) ; HttpURLConnection connection = ( HttpURLConnection ) url. openConnection ( ) ; connection. connect ( ) ; BufferedReader br = new BufferedReader ( new InputStreamReader ( connection. getInputStream ( ) , "UTF-8" ) ) ; String line = null ; while ( null != ( line = br. readLine ( ) ) ) { System . out. println ( line) ; } br. close ( ) ; }
}
场景2:
public static String image2Base64 ( String imgUrl, String token) { URL url; InputStream is = null ; ByteArrayOutputStream outStream = null ; HttpURLConnection httpUrl = null ; try { url = new URL ( imgUrl) ; httpUrl = ( HttpURLConnection ) url. openConnection ( ) ; httpUrl. setDoOutput ( true ) ; httpUrl. setRequestMethod ( "GET" ) ; httpUrl. setRequestProperty ( "Authorization" , token) ; httpUrl. connect ( ) ; httpUrl. getInputStream ( ) ; is = httpUrl. getInputStream ( ) ; outStream = new ByteArrayOutputStream ( ) ; byte [ ] buffer = new byte [ 1024 ] ; int len = 0 ; while ( ( len = is. read ( buffer) ) != - 1 ) { outStream. write ( buffer, 0 , len) ; } return CommonUtils . encode ( outStream. toByteArray ( ) ) ; } catch ( Exception e) { log. error ( "异常信息:" , e) ; } finally { try { if ( is != null ) { is. close ( ) ; } if ( outStream != null ) { outStream. close ( ) ; } } catch ( IOException e) { log. error ( "异常信息:" , e) ; } if ( httpUrl != null ) { httpUrl. disconnect ( ) ; } } return "" ; }