通过块和其他参数使用HttpUrlConnection发送图像

时间:2013-07-31 20:12:07

标签: java android buffer httpurlconnection multipartform-data

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

到目前为止我的代码:

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...

}

非常感谢!

2 个答案:

答案 0 :(得分:2)

那么,

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

stream.skip(chunkId * chunkSize);

我正在跳过几个流的:)。对不起,我的坏。

答案 1 :(得分:0)

您必须定义&#34; multipart / form-data &#34;发送图像(文件)时,而不是表单数据