Java的IO_10对接流

对接流

用:图片——>字节数组——>图片来实践
文件输入流(通过程序做中转),写入字节数组中,程序写入文件中
除了字符串,其他的转接成字符数组需要流来对接

package com.io.cx2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *1、 图片读取到字节数组
 *2、 字节数组写出到文件
 */
public class test09 {

	public static void main(String[] args) {
		//图片转成字节数组
		byte[] datas = fileToByteArray("D:\\EclipsePlace\\IO\\内存泄漏.JPG");
		System.out.println(datas.length);
		byteArrayToFile(datas,"D:\\\\EclipsePlace\\\\IO\\\\tee.jpg");		
	}
	/**
	 * 1、图片读取到字节数组
	 * 1)、图片到程序  FileInputStream
	 * 2)、程序到字节数组	ByteArrayOutputStream
	 */
	public static byte[] fileToByteArray(String filePath) {
		//1、创建源与目的地
		File src = new File(filePath);
		byte[] dest =null;
		//2、选择流
		InputStream  is =null;
		ByteArrayOutputStream baos =null;
		try {
			is =new FileInputStream(src);
			baos = new ByteArrayOutputStream();
			//3、操作 (分段读取)
			byte[] flush = new byte[1024*10]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				baos.write(flush,0,len);		 //写出到字节数组中			
			}		
			baos.flush();
			return baos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if(null!=is) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;		
	}
	/**
	 * 2、字节数组写出到图片
	 * 1)、字节数组到程序 ByteArrayInputStream
	 * 2)、程序到文件 FileOutputStream
	 */
	public static void byteArrayToFile(byte[] src,String filePath) {
		//1、创建源
		File dest = new File(filePath);
		//2、选择流
		InputStream  is =null;
		OutputStream os =null;
		try {
			is =new ByteArrayInputStream(src);
			os = new FileOutputStream(dest);
			//3、操作 (分段读取)
			byte[] flush = new byte[5]; //缓冲容器
			int len = -1; //接收长度
			while((len=is.read(flush))!=-1) {
				os.write(flush,0,len);			//写出到文件	
			}		
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4、释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对接京东云打印需要编写Java代码,并使用京东云打印的API进行调用。以下是一个简单的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class JDCloudPrintIntegration { public static void main(String[] args) { try { // 准备请求的URL和参数 String url = "https://api.jdcloud.com/v1/print/tasks"; String requestData = "{\"printerId\":\"your_printer_id\",\"content\":\"your_print_content\"}"; // 创建URL对象并打开连接 URL obj = new URL(url); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置请求头 connection.setRequestProperty("accept", "application/json"); connection.setRequestProperty("content-type", "application/json"); connection.setRequestProperty("authorization", "your_authorization_token"); // 开始写入请求数据 connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); os.write(requestData.getBytes()); os.flush(); os.close(); // 获取响应结果 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 输出响应结果 System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上代码中,需要将`your_printer_id`替换为你的打印机ID,`your_print_content`替换为需要打印的内容,`your_authorization_token`替换为你的授权令牌。通过调用京东云打印的API,可以实现与京东云打印的对接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值