Java IO 对接流

这篇博客详细介绍了如何使用Java IO进行字节流操作,包括将图片数据存入字节数组以及如何从字节数组写入文件,是理解Java IO流处理的基础教程。
摘要由CSDN通过智能技术生成

完成功能:

  1. 将图片存到字节数组中。
  2. 字节数组写出到文件。
public class IOTest05 {

	public static void main(String[] args) {
		byte[] datas=fileToByteArray("woman.jpg");
		System.out.println(datas.length);
		byteArrayToFile(datas, "p-byte.jpg");
	}
	/**
	 * 1. 将图片存到字节数组中
	 * 1)、图片到程序  FileInputStream
	 * 2)、程序 到字节数组 ByteArrayOutStream
	 */
	public static byte[] fileToByteArray(String filePath) {
		//创建源
		File src=new File(filePath);
		//选择流
		InputStream is=null;
		ByteArrayOutputStream baos=null;
		try {
			is=new FileInputStream(src);
			baos=new ByteArrayOutputStream();
			//操作
			byte[] flush=new byte[1024*40]; //缓冲容器
			int len=-1;  //接收长度
			while ((len=is.read(flush))!=-1) {
				baos.write(flush, 0, len);   //写出到字节数组中
			}
			baos.flush();
			return baos.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(null!=is) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	/**
	 * 2. 字节数组写出到文件
	 * 2.1 字节数组到程序 ByteArrayInputSream
	 * 2.2 程序写出到文件 FileOutStream
	 */
	public static void byteArrayToFile(byte[] src,String filePath) {
		//创建源
		File dest=new File(filePath);
		//选择流
		InputStream is=null;
		OutputStream os=null;
		try {
			is=new ByteArrayInputStream(src);
			os=new FileOutputStream(dest);
			//操作
			byte[] flush=new byte[1024*40]; //缓冲容器
			int len=-1; //接收长度
			while ((len=is.read(flush))!=-1) {
				os.write(flush, 0, len);
			}
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(null!=os) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java对接ChatGPT,您需要使用OpenAI API。您可以使用Java的HTTP客户端库(如Apache HttpClient或OkHttp)来向OpenAI API发送HTTP请求,并解析返回的JSON响应。以下是一个简单的Java代码示例,演示如何使用OpenAI API与ChatGPT进行交互: ```java import java.io.IOException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import org.json.JSONObject; public class ChatGPTClient { // OpenAI API endpoint private static final String API_ENDPOINT = "https://api.openai.com/v1/engines/davinci-codex/completions"; // OpenAI API key private static final String API_KEY = "YOUR_API_KEY_HERE"; // Request content type private static final MediaType CONTENT_TYPE = MediaType.parse("application/json; charset=utf-8"); private final OkHttpClient httpClient; public ChatGPTClient() { this.httpClient = new OkHttpClient(); } public String askChatGPT(String prompt) throws IOException { // Build the request body JSONObject requestBody = new JSONObject(); requestBody.put("prompt", prompt); requestBody.put("max_tokens", 100); requestBody.put("temperature", 0.5); // Build the HTTP request Request request = new Request.Builder() .url(API_ENDPOINT) .header("Authorization", "Bearer " + API_KEY) .post(RequestBody.create(CONTENT_TYPE, requestBody.toString())) .build(); // Send the HTTP request and parse the response try (Response response = httpClient.newCall(request).execute()) { JSONObject responseBody = new JSONObject(response.body().string()); return responseBody.getJSONArray("choices") .getJSONObject(0) .getString("text"); } } } ``` 在上面的代码中,您需要将`YOUR_API_KEY_HERE`替换为您在OpenAI上获取的API密钥。然后,您可以使用`askChatGPT`方法向ChatGPT发送问题,并获得响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值