IO网络传输数据

Inputstream 转 file

InputStream ins = httpUrlConnection.getInputStream();
			try {
				OutputStream os = new FileOutputStream("E:/PDFs/test/test.pdf");
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
					os.write(buffer, 0, bytesRead);
				}
				os.close();
				ins.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			httpUrlConnection.disconnect();```

inputstream 转 bytearray再转base64

InputStream ins = httpUrlConnection.getInputStream();
			ByteArrayOutputStream output = new ByteArrayOutputStream();

			byte[] buffer = new byte[4096];
			int n = 0;
			while (-1 != (n = ins.read(buffer))) {
				output.write(buffer, 0, n);
			}
			byte[] pdfByteArray = output.toByteArray();
			String pdfStr = Base64.getEncoder().encodeToString(pdfByteArray);

String存储为文件

public void save2File(String content,String filePath){
        FileWriter fwriter = null;
        try {
            fwriter = new FileWriter(filePath, true);
            fwriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

发布在服务器读取相对路径

public String getFilePath(String path) throws URISyntaxException {
        URL res = this.getClass().getClassLoader().getResource(path);
        if(res == null){
            System.out.println(path +"  :path can not get file");
        }
        File f = Paths.get(res.toURI()).toFile();
        return f.getAbsolutePath();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值