gpt4 book ai didi

Android - 将字节数组写入 OutputStreamWriter

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:38:08 26 4
gpt4 key购买 nike

我有一个通过网络服务上传照片的应用程序。过去,我将文件加载到流中,然后转换为 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!

最佳答案

改变

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

有了这个

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

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

让我知道它的行为

编辑:根据评论编辑答案

关于Android - 将字节数组写入 OutputStreamWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543227/

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