使用HttpUrlConnection分块发送图片及其他参数

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

}

许多感谢!

你在这个问题上找到解决方案了吗?请告诉我你是怎么做的,我也遇到了同样的问题。谢谢。 - NewDroidDev
@NewDroidDev,这一行代码有误:stream.skip(chunkId * chunkSize);。我已将其删除,现在一切正常。 - Chronos
请问您的param4参数的值是多少? - NewDroidDev
这是我的代码,请帮忙,我时间不够了,我需要完成我的任务。:( http://stackoverflow.com/questions/18200299/how-to-upload-files-using-dataoutputstream?noredirect=1#comment26674565_18200299 - NewDroidDev
2个回答

2

好的,

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

stream.skip(chunkId * chunkSize);

我跳过了流的几个块:)。抱歉我的错误。


它对我不起作用。我需要添加一些代码才能上传文件吗?它没有给我错误,但是它没有通过Web服务器上传文件。请帮帮我,我被卡住了。 - NewDroidDev
请帮我解决这个问题,为什么它无法上传到服务器?我需要添加什么来使这段代码工作吗? - NewDroidDev
我不知道为什么会出现500错误(内部服务器错误)。我认为服务器没有问题,因为当我使用multiPartEntity发送文件时它可以工作,但是我转到这段代码是因为我无法在multiPartEntity中分块我的文件。 - NewDroidDev

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接