package org.springblade.api.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.*;
import java.net.*;
import java.util.Date;
@Slf4j
public class HttpUtil {
	/**
	 * 向指定URL发送POST请求，包含sn、result和file参数
	 *
	 * @param urlString 请求的URL
	 * @param sn      sn参数值
	 * @param result  result参数值
	 * @param file    file参数值，要上传的文件
	 * @return 服务器响应的内容
	 */
	public static String postDataForm(String urlString, String sn, String result, File file) {
		String boundary = Long.toHexString(System.currentTimeMillis()); // 随机边界
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String outputStr = "";

		try {
			URL url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

			try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) {
				// 发送sn参数
				dos.writeBytes(twoHyphens + boundary + lineEnd);
				dos.writeBytes("Content-Disposition: form-data; name=\"sn\"" + lineEnd);
				dos.writeBytes(lineEnd);
				dos.writeBytes(sn);
				dos.writeBytes(lineEnd);

				// 发送result参数
				dos.writeBytes(twoHyphens + boundary + lineEnd);
				dos.writeBytes("Content-Disposition: form-data; name=\"result\"" + lineEnd);
				dos.writeBytes(lineEnd);
				dos.writeBytes(result);
				dos.writeBytes(lineEnd);

				// 发送file参数
				dos.writeBytes(twoHyphens + boundary + lineEnd);
				dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + lineEnd);
				dos.writeBytes("Content-Type: application/octet-stream" + lineEnd);
				dos.writeBytes(lineEnd);

				FileInputStream fis = new FileInputStream(file);
				byte[] buffer = new byte[8192]; // 8k buffer
				int bytesRead;
				while ((bytesRead = fis.read(buffer)) != -1) {
					dos.write(buffer, 0, bytesRead);
				}
				fis.close();
				dos.writeBytes(lineEnd);

				// 结束标记
				dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
				dos.flush();

				// 读取响应
				InputStream is = conn.getInputStream();
				InputStreamReader isr = new InputStreamReader(is, "UTF-8");
				BufferedReader br = new BufferedReader(isr);
				String line;
				while ((line = br.readLine()) != null) {
					outputStr += line + lineEnd;
				}
				br.close();
			}

			conn.disconnect();
		} catch (Exception e) {
			log.error("三方接口调用失败",e);
			e.printStackTrace();
		}

		return outputStr;
	}

	public static void main(String[] args) {
		// 示例：使用postDataForm方法
		String url = "http://121.36.53.58:8099/js/a/mes/mesProductMpp/add";
		String sn = "test666";
		String result = "OK";
		File file = FileUtils.getFileByUrl("https://ai.my-123.cn/pic/product-line/upload/20240714/79bd8c067ec1c132565e19ed0cfc2a58.jpg", ".jpg");
		Date state = new Date();
		System.out.println(state.getTime());
		String response = postDataForm(url, sn, result, file);
		System.out.println("Response: " + response);
		System.out.println(new Date().getTime()-state.getTime());
	}
}
