Android-将字节数组写入OutputStreamWriter

Android-将字节数组写入OutputStreamWriter,android,string,stream,byte,Android,String,Stream,Byte,我有一个通过web服务上传照片的应用程序。在过去,我将一个文件加载到流中,并转换为Base64。然后,我通过OutputStreamWriter的write()方法发布结果字符串。现在,web服务已经更改,它需要多部分/表单数据,而不需要Base64 因此,不知何故,我需要发布这个文件的字符,因为没有转换。我确信我很接近,但我所得到的只是内容长度的下溢或溢出。奇怪的是,在调试器中,我可以看到我的缓冲区长度与我发布的字符串长度相同。下面是我正在做的,希望有足够的代码: // conn is my

我有一个通过web服务上传照片的应用程序。在过去,我将一个文件加载到流中,并转换为Base64。然后,我通过OutputStreamWriter的write()方法发布结果字符串。现在,web服务已经更改,它需要多部分/表单数据,而不需要Base64

因此,不知何故,我需要发布这个文件的字符,因为没有转换。我确信我很接近,但我所得到的只是内容长度的下溢或溢出。奇怪的是,在调试器中,我可以看到我的缓冲区长度与我发布的字符串长度相同。下面是我正在做的,希望有足够的代码:

// conn is my connection
OutputStreamWriter dataStream = new OutputStreamWriter(conn.getOutputStream());

// c is my file
int bytesRead = 0;
long bytesAvailable = c.length();

while (bytesAvailable > 0) {
   byte[] buffer = new byte[Math.min(12288, (int)bytesAvailable)];
   bytesRead = fileInputStream.read(buffer, 0, Math.min(12288, (int)bytesAvailable));

   // assign the string if needed.
   if (bytesRead > 0) {
      bytesAvailable = fileInputStream.available();

      // I've tried many encoding types here.
      String sTmp = new String(buffer, "ISO-8859-1");
      // HERE'S the issue.  I can't just write the buffer,
      dataStream.write(sTmp);
      dataStream.flush();
// Yes there's more code, but this should be enough to show why I don't know what I'm doing!
改变

OutputStreamWriter dataStream = new OutputStreamWriter(conn.getOutputStream());
用这个

DataOutputStream dataStream = new DataOutputStream(conn.getOutputStream()); 
直接调用
dataStream.write(buffer)

让我知道它的表现


编辑:根据注释编辑答案

是否有任何理由将字节转换为字符串,而不是将其原始写入输出流?因为OutputStreamWriter的write()方法接受char[]或string。不是字节[]。请尝试改用
DataOutputStream
。“conn”是httpurl连接。getOutputStream()不返回“DataOutputStream”,也不能强制转换。对不起。我需要咖啡。DataOutputStream数据流=新的DataOutputStream(conn.getOutputStream());对此要谨慎。虽然DataOutputStream允许您将字节[]写入输出,但其他方法与OutputStremWriter不兼容。特别是,可以为OutputStreamWriter提供用于写入字符串的标准编码,其中DataOutputStream具有自己的专有编码。