3

问题是我正在尝试将图像上传到服务器。图像必须按 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...

}

非常感谢!

4

2 回答 2

2

出色地,

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

stream.skip(chunkId * chunkSize);

我跳过了几块流:)。对不起这是我的错。

于 2013-08-12T18:46:09.330 回答
0

发送图像(文件)时,您必须定义“ multipart/form-data ”而不是form-data

于 2014-03-29T10:00:12.127 回答