0

I want to upload the file from my device to php server.But I'm getting the response as internal server error during file upload.what to do for rectifying this error and upload the file correctly. I have tried like this`

try {

              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(selectedFilePath);

              if (sourceFile.isFile()) {

                  try {

                      String upLoadServerUri ="http://www.abservetechdemo.com/mobile/user/cvinsert";
                      // open a URL connection to the Servlet
                      FileInputStream fileInputStream = new FileInputStream(
                              sourceFile);
                      URL url = new URL(upLoadServerUri);


                      conn = (HttpURLConnection) url.openConnection();
                      conn.setDoInput(true);
                      conn.setDoOutput(true);
                      conn.setUseCaches(false);
                      conn.setRequestMethod("POST");
                      conn.setRequestProperty("Connection", "Keep-Alive");
                      conn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
                      conn.setRequestProperty("ENCTYPE",
                              "multipart/form-data");
                      conn.setRequestProperty("Content-Type",
                              "multipart/form-data;boundary=" + boundary);

                      conn.setRequestProperty("cv", selectedFilePath);



                      conn.setRequestProperty("user_id", "27");
                      dos = new DataOutputStream(conn.getOutputStream());

                      dos.writeBytes(twoHyphens + boundary + lineEnd);
                      dos.writeBytes("Content-Disposition: form-data; name=\"cv\";filename=\""
                              + selectedFilePath + "\"" + 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);

                      }

                      dos.writeBytes(lineEnd);
                      dos.writeBytes(twoHyphens + boundary + twoHyphens
                              + lineEnd);

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

                      Log.e("serverResponseMessage",serverResponseMessage);

                      // close the streams //
                      fileInputStream.close();
                      dos.flush();
                      dos.close();

                  } catch (Exception e) {
                      // dialog.dismiss();
                      e.printStackTrace();

                  }
          `

My logcat error

09-21 14:47:35.244 3576-15332/com.abservetech.video E/serverResponseMessage: Internal Server Error
Abserve Tech
  • 117
  • 2
  • 11
  • This is an error on the server side. Check your http servers error log file to see what causes the issue. – arkascha Sep 21 '16 at 08:15
  • ok,I will check that.And one more thing, my android code is correct or not for uploading file along with user id – Abserve Tech Sep 21 '16 at 08:21

0 Answers0