7

我有一个通过网络服务上传照片的应用程序。过去,我将文件加载到流中,然后转换为 Base64。然后我通过 OutputStreamWriter 的 write() 方法发布了结果字符串。现在,Web 服务发生了变化,它需要 multipart/form-data 而不需要 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!
4

1 回答 1

14

改变

OutputStreamWriter dataStream = new OutputStreamWriter(conn.getOutputStream());

有了这个

DataOutputStream dataStream = new DataOutputStream(conn.getOutputStream()); 

并直接调用dataStream.write(buffer);

让我知道它的行为

编辑:根据评论编辑答案

于 2012-07-18T14:24:17.020 回答