1

Why when I upload a file by post from java to a server and I use the following code.

I have edited and added the rest of the top code of the function

     public int uploadFile(String sourceFileUri) {


                  String fileName = sourceFileUri;

                  HttpURLConnection conn = null;
                  DataOutputStream dos = null;  
                  String lineEnd = "\r\n";
                  String twoHyphens = "--";
                  String boundary = "*****";
                  int bytesRead, bytesAvailable, bufferSize;
                  byte[] buffer;
                  int maxBufferSize = 1 * 1024 * 1024; 
                  File sourceFile = new File(sourceFileUri); 

                  if (!sourceFile.isFile()) {

                       runOnUiThread(new Runnable() {
                           public void run() {
                            /*   messageText.setText("Source File not exist :"
                                       +uploadFilePath + "" + uploadFileName);*/
                           }
                       }); 

                       return 0;

                  }
                  else
                  {
                       try { 

              FileInputStream fileInputStream = new FileInputStream(sourceFile);
                           URL url = new URL(upLoadServerUri);
                           int total = (int) sourceFile.length();
                           // Open a HTTP  connection to  the URL
                           conn = (HttpURLConnection) url.openConnection();
                           conn.setFixedLengthStreamingMode(total);
                           conn.setDoInput(true); // Allow Inputs
                           conn.setDoOutput(true); // Allow Outputs
                           conn.setUseCaches(false); // Don't use a Cached Copy
                           conn.setRequestMethod("POST");
                           conn.setRequestProperty("Connection", "Keep-Alive");
                           conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                           conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                           conn.setRequestProperty("uploaded_file", fileName); 
                           conn.setRequestProperty("mail", MAIL); 
                           conn.setRequestProperty("OS", "1");
                           conn.setRequestProperty("LANG", "ES");

  dos = new DataOutputStream(conn.getOutputStream());

                   dos.writeBytes(twoHyphens + boundary + lineEnd); 
                   dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                             + fileName + "\"" + lineEnd);

                   dos.writeBytes(lineEnd);

                   // create a buffer of  maximum size
                   bytesAvailable = fileInputStream.available(); 

                   bufferSize = Math.min(bytesAvailable, maxBufferSize);
                   buffer = new byte[bufferSize];

                   // read file and write it into form...
                   bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

                   while (bytesRead > 0) {

                     dos.write(buffer, 0, bufferSize);
                     bytesAvailable = fileInputStream.available();
                     bufferSize = Math.min(bytesAvailable, maxBufferSize);
                     bytesRead = fileInputStream.read(buffer, 0, bufferSize);   

                    }

                   // send multipart form data necesssary after file data...
                   dos.writeBytes(lineEnd);
                   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                   // Responses from the server (code and message)
                   serverResponseCode = conn.getResponseCode();

The bytes excpected and sended does not match? I don't understand why it happens.

Exception : expected 589715 bytes but received 589840
java.io.IOException: expected 589715 bytes but received 589840
at libcore.net.http.FixedLengthOutputStream.write(FixedLengthOutputStream.java:39)
at java.io.DataOutputStream.write(DataOutputStream.java:98)
at com.androidexample.uploadtoserver.UploadToServer.uploadFile(UploadToServer.java:152)
at com.androidexample.uploadtoserver.UploadToServer$1.run(UploadToServer.java:62)
at java.lang.Thread.run(Thread.java:856)
Ridcully
  • 23,362
  • 7
  • 71
  • 86
David
  • 373
  • 5
  • 23

1 Answers1

2

You don't show where you are writing the content to the stream. I suspect you need to account for the boundary byte length as well as the file length. FYI: You don't have to cast to an int. There is HttpURLConnection.setFixedLengthStreamingMode(long) method. You might also want to take a look at this other thread.

EDIT: According to the exception, you are off by 125 bytes (589840 - 589715). Check to see if 2x boundary length (this would be the number of bytes from the resulting String twoHyphens + boundary + lineEnd) plus "Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + fileName + "\"" + lineEnd bytes is 125 bytes. If it is, then that's the extra content you have to account for.

Community
  • 1
  • 1
MadConan
  • 3,749
  • 1
  • 16
  • 27
  • 1
    Still not seeing where you are writing to the connection. That's what counts. You can say you are going to write X bytes, but end up writing Y. Where is the code like `conn.connect(); OutputStream out = conn.getOutputStream(); ... out.write(byteArary,0,bytesArray.length];` etc. – MadConan Oct 29 '13 at 12:20
  • Sorry I have fixed it – David Oct 29 '13 at 12:32
  • I tried but I still having same issue -> E/Upload file to server Exception(4535): Exception : expected 892819 bytes but received 892944 my total is int total = (int) sourceFile.length();. Why it expects less than received, really I don't undertand nothing.. – David Oct 29 '13 at 16:13