MultipartUtility Android Extra字节

时间:2018-02-20 17:19:15

标签: java android asp.net-web-api upload

有人能告诉我在下面的代码中我做错了什么吗?我将部分文件从Android / Java发送到Asp.Net C#Web API。

服务器端收到的字节数组有两个额外的字节,它们打破了文件的校验和。

我正在使用此处发布的MultipartUtility类:simple HttpURLConnection POST file multipart/form-data from android to google blobstore

这是我将代码字节数发送到服务器的代码:

 public void addFilePart(String fieldName, byte[] stream) throws IOException {
    String fileName = "file";
    writer.append("--" + boundary).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE_FEED);
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
    writer.append(LINE_FEED);
    writer.flush();

    outputStream.write(stream);
    outputStream.flush();

    writer.append(LINE_FEED);
    writer.flush();
}
public List<String> finish() throws IOException {
    List<String> response = new ArrayList<String>();
    writer.append(LINE_FEED);
    writer.flush();
    writer.append("--" + boundary + "--");
    writer.append(LINE_FEED);
    writer.flush();
    writer.close();

    // checks server's status code first
    int status = httpConn.getResponseCode();
    if (status == HttpURLConnection.HTTP_OK) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpConn.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            response.add(line);
        }
        reader.close();
        httpConn.disconnect();
    } else {
        throw new IOException("Server returned non-OK status: " + status);
    }
    return response;
}

如果需要,我可以发布全班,这真的是上面的链接。

感谢您的帮助。 肖恩

0 个答案:

没有答案