gpt4 book ai didi

java - 通过 block 和其他参数使用 HttpUrlConnection 发送图像

转载 作者:太空狗 更新时间:2023-10-29 13:29:08 26 4
gpt4 key购买 nike

问题是我正在尝试将图像上传到服务器。图像必须按 256kb 的 block 上传,我需要在每次调用时传递 block 数和 ID。我可以获得要上传的 block 总数,并且我正在使用 BufferedInputStream 来获取 block 字节。但是当我完成所有 block 的上传时,显示的图像总是损坏。

到目前为止我的代码:

int chunkSize = 255 * 1024;
final long size = mFile.length();
final long chunks = mFile.length() < chunkSize? 1: (mFile.length() / chunkSize);

int chunkId = 0;

BufferedInputStream stream = new BufferedInputStream(new FileInputStream(mFile));

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "RQdzAAihJq7Xp1kjraqf";// random data

for (chunkId = 0; chunkId < chunks; chunkId++) {

URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(20000 /* milliseconds */);
conn.setConnectTimeout(20000 /* milliseconds */);


// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");

conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);

String param1 = ""+chunkId;
String param2 = ""+chunks;
String param3 = mFile.getName();

// for every param
dos.writeBytes("Content-Disposition: form-data; name=\"chunk\"" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes("Content-Length: " + param1.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(param1 + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

// Send parameter #chunks
dos.writeBytes("Content-Disposition: form-data; name=\"chunks\"" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes("Content-Length: " + param2.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(param2 + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);


// Send parameter #name
dos.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
dos.writeBytes("Content-Length: " + param4.length() + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(param3 + lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

// Send parameter #file
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + param4 + "\"" + lineEnd); // filename is the Name of the File to be uploaded

dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
dos.writeBytes(lineEnd);

byte[] buffer = new byte[chunkSize];

stream.skip(chunkId * chunkSize);
stream.read(buffer);

// dos.write(buffer, 0, bufferSize);
dos.write(buffer);


dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.flush();
dos.close();


// read response...

}

非常感谢!

最佳答案

嗯,

我解决了这个问题。我删除了以下行:

stream.skip(chunkId * chunkSize);

我跳过了几个流 block :)。对不起我的错。

关于java - 通过 block 和其他参数使用 HttpUrlConnection 发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17980004/

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