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?

    2 answers 2

    In fact, the problem was only that I created an array of bytes of a specific size, but did not fill it with anything ... Therefore, on the server I received an empty file, it was necessary to add this line to the code

     try { fis = new FileInputStream(zipToSend); bos = new BufferedInputStream(fis); ----> bos.read(zipPhotosBytes); } 

    And after that I was able to get the full file.

      For a zip file it will be more correct to use this .

      • more efficiently than bos = new BufferedOutputStream (urlConnection.getOutputStream ()); ? - Aleksey Timoshchenko
      • I don’t know what is more effective, but it’s simpler in that ziping will take place under the hood - Anton Klimenko
      • So I do not need to zip it, I already pass File zipToSend = new File (zipPath); The path to the file that is zipped ... Or do I not understand it? - Aleksey Timoshchenko
      • you can not use ZipOutputStream. However, you wrote that you zipuet a picture and send it to the server. So, you can zip through ZipOutputStream. Here is a great example of filling an array of bytes. - Anton Klimenko