С начало получаю upload_url, потом пытаюсь залить файл POST запросом.
При попытки залить файла у меня возникает ошибка.
{"error_msg":"one.image.server.upload.ContentUploadServerException: BAD_REQUEST","error_code":"4","error_data":"BAD_REQUEST"}
Код Android
public HttpURLConnection postImage(String upload_url, String pathFile) throws IOException {
URL urlCon = new URL(upload_url);
HttpURLConnection urlConnection = (HttpURLConnection) urlCon.openConnection();
urlConnection.setRequestMethod(POST);
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setReadTimeout(35000);
urlConnection.setConnectTimeout(35000);
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (U; Android) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
urlConnection.setRequestProperty("Connection", "keep-alive");
File file = new File(pathFile);
String fileName = file.getName();
String extension = MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
String mime = "image/jpeg";
if (extension != null)
mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Log.e("fileName", "filename = " + fileName + " exist = " + file.exists() + " mime = " + mime);
urlConnection.setRequestProperty("Content-Type", mime);
urlConnection.setRequestProperty("Content-Length", String.valueOf(file.length()));
OutputStream os = urlConnection.getOutputStream();
urlConnection.connect();
BufferedOutputStream bos = new BufferedOutputStream(os);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buff = new byte[1024];
int i;
while ((i = bis.read(buff, 0, buff.length)) > 0) bos.write(buff, 0, i);
bis.close();
bos.flush();
bos.close();
os.close();
int responseCode = urlConnection.getResponseCode();
Log.e(TAG, String.format("response code: %d request: %s %s", responseCode, urlConnection.getRequestMethod(), urlConnection.getURL().toString()));
return urlConnection;
}
Выбранная версия SDK Android 6.0 по этому использую HttpURLConnection
Чисто по HttpUrlConnection пробовал разные методы и записывать еще в начало потоко инфу. Но помог другой метод, правда для Android - MultipartEntity лучше не использовать, но что бы работало использую
@SuppressWarnings({"deprecation"})
private static String multipost(String upload_url, File file) {
try {
URL url = new URL(upload_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("picture", new FileBody(file));
conn.setRequestProperty("Connection", "Keep-Alive");
conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
OutputStream os = conn.getOutputStream();
reqEntity.writeTo(conn.getOutputStream());
os.close();
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(conn.getInputStream());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}