Can anybody tell me what I'm doing wrong in the code below? I'm sending part of the file from Android/Java to Asp.Net C# Web API.
the array of bytes received on the server side has two extra bytes that are breaking the checksum of the file.
I'm using the MultipartUtility class posted here: simple HttpURLConnection POST file multipart/form-data from android to google blobstore
here is my code to send the array of bytes to the server:
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;
}
I can post the whole class if needed, it's really the link above.
Thank you for your help. Sean