Java将单个字节写入DataOutputStream

时间:2014-01-03 19:31:31

标签: java

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

/* 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]中的一个字节)之后,它获得了一个超出范围的索引错误。我似乎无法找出原因。

任何澄清或帮助表示赞赏。谢谢。

3 个答案:

答案 0 :(得分:2)

看看你的代码:

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

您将长度为TemporaryByteArray(一)的数组1传递给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);而不是逐字节。

答案 1 :(得分:1)

我受过良好教育的猜测是问题在于:

The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);

write(byte[], int, int)的参数是:

  • 一个byte[] 缓冲区,其中包含要写入的数据
  • int 偏移量,用于告知缓冲区中您想要的数据何处开始
  • int 长度,表示您要写入的字节数

问题在于传递给offset参数的值。它是CurrentPostByte,因此在第二次迭代中,您告诉它从索引TemporaryByteArray开始阅读1。但是,TemporaryByteArray始终是一个单元素数组,它只在索引0处有一个项目。

你应该纠正:

The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, TemporaryByteArray.length);

或者更简单地说:

The_Post_Request_Write_Stream.write(TemporaryByteArray);

答案 2 :(得分:0)

你没有说哪一行抛出异常,但我猜它是

byte[] TemporaryByteArray = new byte[]{BuyData[CurrentPostByte]};

看起来你想在这里引用PostData。