Transfer Audio-File from Client to Http Server via URLConnection
<p>i am currently working on a programming-project in my school. I need to send an audio file (MIDI format) from the Client successfully to a Http Server. I already tried to do this myself and did much research on the internet and in the Stackoverflow forum. Currently it is possible to send the file from the client to the server, but on the server side, the audio file is not playable.</p>

<p>The following is the client-side-code:</p>

<pre><code>private static void sendPOST() throws IOException{
    final int mid = 1;
    final String POST_URL = "http://localhost:8080/musiker/hörprobe?mid="+mid;
    final File uploadFile = new File("C://Users//Felix Ulbrich//Desktop//EIS Prototype MIDIs//Pop//BabyOneMoreTime.mid");
    String boundary = Long.toHexString(System.currentTimeMillis()); 
    String CRLF = "\r\n";
    String charset = "UTF-8";
    URLConnection connection = new URL(POST_URL).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    try (
            OutputStream output = connection.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
        ){
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + uploadFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(uploadFile.getName())).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        Files.copy(uploadFile.toPath(), output);
        output.flush();
        writer.append(CRLF).flush();

        writer.append("--" + boundary + "--").append(CRLF).flush();

        int responseCode = ((HttpURLConnection) connection).getResponseCode();
        System.out.println(responseCode);
        }
}
</code></pre>

<p>The following is the server-side-code:</p>

<pre><code>int FILE_SIZE = Integer.MAX_VALUE-2;
                    int bytesRead = 0;
                    int current = 0;
                    FileOutputStream fos = null;
                    BufferedOutputStream bos = null;
                    byte[] mybytearray = new byte[FILE_SIZE];
                    String FILE_TO_RECEIVED = "C://root//m"+musikerid+"hp"+(hörprobenzaehler+1)+".mid";
                    File f = new File(FILE_TO_RECEIVED);
                    if(!f.exists()){
                        f.createNewFile();
                    }
                    InputStream input = t.getRequestBody();
                    fos = new FileOutputStream(FILE_TO_RECEIVED);
                    bos = new BufferedOutputStream(fos);
                    bytesRead = input.read(mybytearray,0,mybytearray.length);
                    current = bytesRead;
                    do{
                        bytesRead = input.read(mybytearray, current, mybytearray.length-current);
                        if(bytesRead &gt;= 0){
                            current += bytesRead;
                        }
                    }while(bytesRead&gt;-1);

                    bos.write(mybytearray,0,current);
                    bos.flush();
                    fos.close();
                    bos.close();
                    t.sendResponseHeaders(200, 0);
                    input.close();
</code></pre>

<p>I am pretty desperate right now, because i couldn't find any solution to this problem. I need to use an HTTP server, but i don't need to use the TCP protocol (which is used right now via streams). I thought about a solution via ftp so i don't need to convert the file to a byte-array first. I assume that the problem lies exactly there. The server can't create the audio-file (midi-file) correctly from the byte-array. If anyone of you knows of a solution. Pls, i need your help :D</p>

<p>Greetings, Gizpo</p>