Java 通过区块和其他参数发送带有HttpUrlConnection的图像
问题是我正试图上传一个图像到服务器上。 图像必须以256kb的块上传,我需要在每次调用时传递块计数和id。 我可以得到要上传的块的总数,我正在使用BufferedInputStream来获得块字节。 但是当我上传完所有的区块后,显示的图像总是被破坏了 到目前为止,我的代码是:Java 通过区块和其他参数发送带有HttpUrlConnection的图像,java,android,buffer,httpurlconnection,multipartform-data,Java,Android,Buffer,Httpurlconnection,Multipartform Data,问题是我正试图上传一个图像到服务器上。 图像必须以256kb的块上传,我需要在每次调用时传递块计数和id。 我可以得到要上传的块的总数,我正在使用BufferedInputStream来获得块字节。 但是当我上传完所有的区块后,显示的图像总是被破坏了 到目前为止,我的代码是: int chunkSize = 255 * 1024; final long size = mFile.length(); final long chunks = mFile.length() < chunkSize
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...
}
int chunkSize=255*1024;
最终长尺寸=mFile.length();
final long chunks=mFile.length()
非常感谢 嗯
我解决了这个问题。
我删除了以下行:
stream.skip(chunkId * chunkSize);
我跳过了流的几个部分:)。抱歉,我的问题。发送图像(文件)时,您必须定义“多部分/表单数据””而不是表单数据。您找到了解决此问题的方法吗?请告诉我您做了什么?我也遇到了同样的问题。Thanks@NewDroidDev,此行错误:stream.skip(chunkId*chunkSize);。我删除了它,现在一切正常。我可以知道你的param4值多少吗?看,这是我的代码。请帮忙,我没有时间完成我的任务(这对我不起作用。我是否需要在中添加一些代码才能上载文件。它不会给我错误,但不会通过web服务器上载文件。请帮助我,我是stack。请帮助我解决这个问题,为什么它不会上载到服务器是我需要添加的任何内容,以使此代码正常工作?我不知道为什么会出现错误500。)(内部服务器错误)。我认为服务器没有问题,因为当我使用multiPartEntity发送文件时,它可以工作,但我使用此代码,因为我无法在multiPartEntity中分块我的文件