用url类来访问服务器上的文件

场景一:
java 复制代码
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.setRequestMethod("GET");
        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:
java 复制代码
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();
            //创建一个Buffer字符串
            byte[] buffer = new byte[1024];
            //每次读取的字符串长度,如果为-1,代表全部读取完毕
            int len = 0;
            //使用一个输入流从buffer里把数据读取出来
            while ((len = is.read(buffer)) != -1) {
                //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                outStream.write(buffer, 0, len);
            }
            // 对字节数组Base64编码
            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 "";
    }
相关推荐
东北赵四5 分钟前
JVM实践(调优)
java·jvm
we1less8 分钟前
[audio] AudioTrack (五) 共享内存创建分析
android·java·开发语言
一 乐9 分钟前
景区管理|基于springboot + vue景区管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
幽络源小助理10 分钟前
SpringBoot+Vue大型商场应急预案管理系统源码 | Java安全类项目免费下载 – 幽络源
java·vue.js·spring boot
lbb 小魔仙11 分钟前
【Java】Spring Boot 与 Spring Cloud 整合:微服务架构入门实战
java·spring boot·spring cloud·架构
JIngJaneIL12 分钟前
基于java + vue连锁门店管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
骥龙16 分钟前
第四篇:融合篇——架构的涌现效应:1+1>2
运维·架构·云计算
月明长歌16 分钟前
怎么把 SQL 的增删改查写成“稳、准、可维护”的
java·数据库·sql
南汐以墨18 分钟前
UI自动化测试指南(二):常用方法
java·测试工具
qq_3106585118 分钟前
webrtc源码走读(三)核心引擎层——音频引擎
服务器·c++·音视频·webrtc