I need to send a picture to the server. Here is the code that does it.
I put the picture in zip aphiv, then this archive is laid out on byte[] like this
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(); } } and I get byte[] zipPhotosBytes which stores the full stream of bytes and I already pass it in the method
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; } But for some reason, on the server, a file is simply created that cannot be opened, nothing can be done with it ... Although the weight of the file that I transfer and which is obtained on the server is the same, then the bytes are not lost ...
Maybe I need to add something to the code?