4

I am uploading audio file from my android application to server.Upload code is running on my side but not file is not getting upload on server.Here is the code what i m using .I have also commented code by which file is getting uploaded .here is the code :

public void UploadFile(String path, String id) { HttpURLConnection connection = null; DataOutputStream outputStream = null; String pathToOurFile = "" + path; String urlServer = "http://txtapi.com/musicApp/uploadfile.php?fileId=" + id + "&sharedBy=" + sp.getString(CommonUtility.phone, CommonUtility.getPhoneNumber(ct)); String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; try { FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"fileId\";filename=\"" + path + "\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); // Read file // bytesRead = fileInputStream.read(buffer, 0, bufferSize);//file is getting upload by this code // int i =0; // while (bytesRead > 0) // { // i++; // // outputStream.write(buffer, 0, bufferSize); // bytesAvailable = fileInputStream.available(); // bufferSize = Math.min(bytesAvailable, maxBufferSize); // bytesRead = fileInputStream.read(buffer, 0, bufferSize); // } buffer = new byte[1024]; int bufferLength = 0; while ((bufferLength = fileInputStream.read(buffer)) > 0) { //add the data in the buffer to the file in the file output stream (the file on the sd card Log.i("bytes available ", "" + fileInputStream.available()); Log.i("buffer length", "" + bufferLength); outputStream.write(buffer); //add up the size so we know how much is downloaded } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); BugSenseHandler.setLogging(true); //Exception handling Log.i("Inside catch of ", "Inside catch of upload "); BugSenseHandler.sendException(ex); } finally { System.gc(); } } 

1 Answer 1

1

The amount of bytes read should be the amount of bytes written to the outputstream so change it to following:

 while ((bufferLength = fileInputStream.read(buffer)) > 0) { //add the data in the buffer to the file in the file output stream (the file on the sd card Log.i("bytes available ", "" + fileInputStream.available()); Log.i("buffer length", "" + bufferLength); outputStream.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded } 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply but I have read somewhere that "outputStream.write(buffer, 0, bufferLength)"is equivalent to "outputStream.write(buffer)".Thats why I have changed that line
no its not because when you only provide buffer as an argument then it will write the whole buffer to the stream. And when you are writing the last part of the file it will not be always 1024 as your buffer length. For example lets say your file size is 1026 Bytes now your while loop will read 1024 on the first pass and then you need to read the last 2 bytes from the file and similarly write to the outputstream so you need to write 0, 2 because there is only last two bytes you want to write...now in your case it will write the 1024 buffer again if you dont provide the length

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.