【问题标题】:Send image with HttpUrlConnection by chunks and other parameters通过块和其他参数发送带有 HttpUrlConnection 的图像
【发布时间】:2013-07-31 20:12:07
【问题描述】:

问题是我正在尝试将图像上传到服务器。 图像必须按 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,这一行是错误的:stream.skip(chunkId * chunkSize);。我删除了它,现在一切正常。
  • 我可以知道你的 param4 的值是多少吗?
  • 看这里是我的代码。请帮助我没有时间完成我的任务。 :(stackoverflow.com/questions/18200299/…

标签: java android buffer httpurlconnection multipartform-data


【解决方案1】:

嗯,

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

stream.skip(chunkId * chunkSize);

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

【讨论】:

  • 它对我不起作用。我是否需要添加一些代码才能上传文件。它不会给我错误,但不会通过网络服务器上传文件。请帮帮我,我是堆栈。
  • 请帮我解决这个问题,为什么它不上传到服务器我需要添加什么才能使这段代码工作?
  • 我不知道为什么会收到错误 500(内部服务器错误)。我认为服务器没有问题,因为当我使用 multiPartEntity 发送文件时它可以工作,但我转到此代码,因为我无法在 multiPartEntity 中分块我的文件
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2017-07-04
    相关资源
    最近更新 更多