Мне нужно передать картинку на сервер вот код который это делает
Я картинку вкладываю в zip
apхив, потом этот архив раскладываю на byte[]
вот так
File zipToSend = new File(zipPath);
byte[] zipPhotosBytes = new byte[(int) zipToSend.length()];
FileInputStream fis = null;
BufferedInputStream bos = null;
try {
fis = new FileInputStream(zipToSend);
bos = new BufferedInputStream(fis);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
и у меня получается byte[] zipPhotosBytes
который хранит полный поток байтов и его я уже передаю в методе
public static JSONObject sentJsonToServer(final URL url, final byte[] data) {
ExecutorService ex = Executors.newCachedThreadPool();
Future<JSONObject> objectFuture = ex.submit(new Callable<JSONObject>() {
@Override
public JSONObject call() throws Exception {
BufferedOutputStream bos = null;
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/zip");
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
bos = new BufferedOutputStream(urlConnection.getOutputStream());
bos.write(data);
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// returns POST request
if (urlConnection != null) {
return getJSONFromUrl(urlConnection);
} else {
throw new NullPointerException();
}
}
});
JSONObject responseJson = null;
try {
responseJson = objectFuture.get();
} catch (InterruptedException | ExecutionException | NullPointerException e) {
e.printStackTrace();
}
return responseJson;
}
Но на сервере почему то формируется просто файл который нельзя ни открыть, ничего нельзя с ним сделать... Хотя вес файла который я передаю и который получается на сервере совпадает, значит байты не теряются...
Может мне нужно что то добавить в код ?