gpt4 book ai didi

Java 将单个字节写入 DataOutputStream

转载 作者:行者123 更新时间:2023-11-29 05:31:33 26 4
gpt4 key购买 nike

我正在为 HTTP post 请求将单个字节写入 java 中的 DataOutputStream。帖子的结构如下:

/* Init Post */
URL PostToLink = new URL(GeneralArguments.get("PostLink_String"));
byte[] PostData = PutKeyedPostArgs.get("PostBody").getBytes("UTF-8");
HttpURLConnection Post_Request_Connection = (HttpURLConnection) PostToLink.openConnection();
Post_Request_Connection.setDoOutput(true);
Post_Request_Connection.setDoInput(false);
Post_Request_Connection.setRequestMethod("POST");
//Post_Request_Connection.setRequestProperty("charset", "utf-8");
Post_Request_Connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Post_Request_Connection.setRequestProperty("Content-Length", Integer.toString(PostData.length));
Post_Request_Connection.setRequestProperty("Connection", "Keep-Alive");
Post_Request_Connection.setRequestProperty("User-Agent", UserAgent); // Defined earlier
Post_Request_Connection.setRequestProperty("Cookie", CookieVal); // Defined earlier
Post_Request_Connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
Post_Request_Connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
Post_Request_Connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
Post_Request_Connection.setInstanceFollowRedirects(false);
Post_Request_Connection.setUseCaches(false);

/* Obtain Write Stream */
DataOutputStream The_Post_Request_Write_Stream = new DataOutputStream(Post_Request_Connection.getOutputStream());
JOptionPane.showMessageDialog(null, PostData.length); // For Debugging

JOptionPane.showMessageDialog(null, "before for"); // For Debugging
/* Begin writing byte-by-byte to output stream */
for(int CurrentPostByte = 0; CurrentPostByte < PostData.length; CurrentPostByte++){
JOptionPane.showMessageDialog(null, CurrentPostByte); // For Debugging
byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);
/* Length should always be 1 */
}

出于某种原因,它在写入第二个字节(位于 PostData[1] 的字节)后出现索引越界错误。我似乎无法找出原因。

如有任何澄清或帮助,我们将不胜感激。谢谢。

最佳答案

看看你的代码:

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);

您正在将长度为 1(一)的数组 TemporaryByteArray 传递给 write 方法,但使用对 PostData 有效的索引> 仅数组。

您可以通过将其更改为以下方式来修复代码:

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, TemporaryByteArray.length);

或者,更简单

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, 1);

或者,更简单

The_Post_Request_Write_Stream.write(PostData, CurrentPostByte, 1);

但是,当然,最好的解决方案是删除无意义的循环并立即写入整个数组,The_Post_Request_Write_Stream.write(PostData); 而不是逐字节写入。

关于Java 将单个字节写入 DataOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20911321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com