title: Uploading Pdf to server using Retrofit 2
body: <p>I am trying to upload Pdf files to server using <strong>Retrofit 2.0</strong> and store their path to my MySQL database. I am successful in storing the path but the pdf that is supposed to be stored in my home directory is corrupted.  </p>
  
  <p><strong>upload.php</strong></p>
  
  <pre><code>&lt;?php
  
  require "init.php";
  
  if($con)
  {
      $title=$_POST['title'];
      $pdf=$_POST['pdf'];
  
      //Getting the server ip
      $server_ip = gethostbyname(gethostname());
  
      $upload_path="uploads/$title.pdf";
  
      $upload_url = 'http://'.$server_ip.'/pdfupload1/'.$upload_path;
  
      $sql="insert into pdfinfo1(title,path) values('$title','$upload_url');";
  
          if(mysqli_query($con,$sql))
          {
              file_put_contents($upload_path,$pdf);
  
              echo json_encode(array('response'=&gt;"Pdf Uploaded Successfully"));
  
          }
          else
          {
              echo json_encode(array('response'=&gt;"Pdf Upload Failed"));
  
          }
          mysqli_close($con);
  }
  ?&gt;
  </code></pre>
  
  <p><strong>ApiClient.java</strong></p>
  
  <pre><code>    public class ApiClient
  {
  
      private static final String BaseUrl="http://10.0.2.2/pdfupload1/";
  
      private static Retrofit retrofit;
  
      public static Retrofit getApiClient()
      {
          if(retrofit==null)
          {
              retrofit=new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
          }
          return retrofit;
      }
  }
  </code></pre>
  
  <p><strong>ApiInterface.java</strong></p>
  
  <pre><code>    public interface ApiInterface
  {
      @Multipart
      @POST("upload.php")
      Call&lt;PdfClass&gt; PdfUploadFunction (@Part("title") RequestBody title, @Part("pdf") RequestBody image);
  }
  </code></pre>
  
  <p><strong>PdfClass.java</strong></p>
  
  <pre><code>   public class PdfClass
  {
      //This is a Model Class Retrofit
  
      @SerializedName("title")
      private String Title;
  
      @SerializedName("pdf")
      private String Pdf;
  
      @SerializedName("response")
      private String Response;
  
      public String getResponse() {
          return Response;
      }
  }
  </code></pre>
  
  <p><strong>FilePath.java</strong></p>
  
  <pre><code>   public class FilePath {
  
      /**
       * Method for return file path of Gallery image
       *
       * @param context
       * @param uri
       * @return path of the selected image file from gallery
       */
  
      public static String getPath(final Context context, final Uri uri)
      {
          //check here to KITKAT or new version
          final boolean isKitKat = Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT;
  
          // DocumentProvider
          if (isKitKat &amp;&amp; DocumentsContract.isDocumentUri(context, uri)) {
  
              // ExternalStorageProvider
              if (isExternalStorageDocument(uri)) {
                  final String docId = DocumentsContract.getDocumentId(uri);
                  final String[] split = docId.split(":");
                  final String type = split[0];
  
                  if ("primary".equalsIgnoreCase(type)) {
                      return Environment.getExternalStorageDirectory() + "/" + split[1];
                  }
              }
  
              //DownloadsProvider
              else if (isDownloadsDocument(uri)) {
  
                  final String id = DocumentsContract.getDocumentId(uri);
                  final Uri contentUri = ContentUris.withAppendedId(
                          Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  
                  return getDataColumn(context, contentUri, null, null);
              }
  
              // MediaProvider
              else if (isMediaDocument(uri)) {
                  final String docId = DocumentsContract.getDocumentId(uri);
                  final String[] split = docId.split(":");
                  final String type = split[0];
  
                  Uri contentUri = null;
                  if ("image".equals(type)) {
                      contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                  } else if ("video".equals(type)) {
                      contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                  } else if ("audio".equals(type)) {
                      contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                  }
  
                  final String selection = "_id=?";
                  final String[] selectionArgs = new String[] {
                          split[1]
                  };
  
                  return getDataColumn(context, contentUri, selection, selectionArgs);
              }
          }
          // MediaStore (and general)
          else if ("content".equalsIgnoreCase(uri.getScheme())) {
  
              // Return the remote address
              if (isGooglePhotosUri(uri))
                  return uri.getLastPathSegment();
  
              return getDataColumn(context, uri, null, null);
          }
          // File
          else if ("file".equalsIgnoreCase(uri.getScheme())) {
              return uri.getPath();
          }
  
          return null;
      }
  
      /**
       * Get the value of the data column for this Uri. This is useful for
       * MediaStore Uris, and other file-based ContentProviders.
       *
       * @param context The context.
       * @param uri The Uri to query.
       * @param selection (Optional) Filter used in the query.
       * @param selectionArgs (Optional) Selection arguments used in the query.
       * @return The value of the _data column, which is typically a file path.
       */
      public static String getDataColumn(Context context, Uri uri, String selection,
                                         String[] selectionArgs) {
  
          Cursor cursor = null;
          final String column = "_data";
          final String[] projection = {
                  column
          };
  
          try {
              cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                      null);
              if (cursor != null &amp;&amp; cursor.moveToFirst()) {
                  final int index = cursor.getColumnIndexOrThrow(column);
                  return cursor.getString(index);
              }
          } finally {
              if (cursor != null)
                  cursor.close();
          }
          return null;
      }
  
      /**
       * @param uri The Uri to check.
       * @return Whether the Uri authority is ExternalStorageProvider.
       */
      public static boolean isExternalStorageDocument(Uri uri) {
          return "com.android.externalstorage.documents".equals(uri.getAuthority());
      }
  
      /**
       * @param uri The Uri to check.
       * @return Whether the Uri authority is DownloadsProvider.
       */
      public static boolean isDownloadsDocument(Uri uri) {
          return "com.android.providers.downloads.documents".equals(uri.getAuthority());
      }
  
      /**
       * @param uri The Uri to check.
       * @return Whether the Uri authority is MediaProvider.
       */
      public static boolean isMediaDocument(Uri uri) {
          return "com.android.providers.media.documents".equals(uri.getAuthority());
      }
  
      /**
       * @param uri The Uri to check.
       * @return Whether the Uri authority is Google Photos.
       */
      public static boolean isGooglePhotosUri(Uri uri) {
          return "com.google.android.apps.photos.content".equals(uri.getAuthority());
      }
  }
  </code></pre>
  
  <p><strong>FirstActivity.java</strong></p>
  
  <pre><code>  public class FirstActivity extends AppCompatActivity {
      Button button1,button2;
      EditText imagename;
      ImageView imageView;
      private static final int PDF_REQUEST=777;
      private Bitmap bitmap;
  
      public int PDF_REQ_CODE = 1;
  
      String PdfNameHolder, PdfPathHolder, PdfID;
      Uri uri;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_first);
  
          button1=(Button)findViewById(R.id.button);
          button2=(Button)findViewById(R.id.button2);
  
          imagename=(EditText)findViewById(R.id.editText1);
          //imageView=(ImageViewfindViewById(R.id.imageView1);
  
  
  
  
          button1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  //selectImage();
                  selectPdf();
  
  
              }
          });
  
          button2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  // uploadImage();
                  PdfUploadFunction();
  
              }
          });
  
      }
  
      private void selectPdf()
      {
          Intent intent = new Intent();
  
          intent.setType("application/pdf");
  
          intent.setAction(Intent.ACTION_GET_CONTENT);
  
          startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if (requestCode == PDF_REQ_CODE &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
  
              uri = data.getData();
  
          }
      }
  
      public void PdfUploadFunction() {
  
          PdfNameHolder = imagename.getText().toString();
  
          PdfPathHolder = FilePath.getPath(this, uri);
  
          //File file=new File(uri.getPath());
  
  
  
  
              RequestBody Title=RequestBody.create(MediaType.parse("text/plain"),imagename.getText().toString());
              RequestBody Pdf=RequestBody.create(MediaType.parse("application/pdf"), PdfPathHolder);
              ApiInterface apiInterface=ApiClient.getApiClient().create(ApiInterface.class);
              Call&lt;PdfClass&gt; call=apiInterface.PdfUploadFunction(Title,Pdf);
  
              call.enqueue(new Callback&lt;PdfClass&gt;() {
                  @Override
                  public void onResponse(Call&lt;PdfClass&gt; call, Response&lt;PdfClass&gt; response) {
  
                      PdfClass pdfClass=response.body();
                      Toast.makeText(getApplicationContext(),"Server Response: "+pdfClass.getResponse(),Toast.LENGTH_LONG).show();
                  }
  
                  @Override
                  public void onFailure(Call&lt;PdfClass&gt; call, Throwable t) {
  
                  }
              });
  
      }
  
      public void AllowRunTimePermission(){
  
          if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE))
          {
  
              Toast.makeText(this,"READ_EXTERNAL_STORAGE permission Access Dialog", Toast.LENGTH_LONG).show();
  
          } else {
  
              ActivityCompat.requestPermissions(this,new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
  
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int RC, String per[], int[] Result) {
  
          switch (RC) {
  
              case 1:
  
                  if (Result.length &gt; 0 &amp;&amp; Result[0] == PackageManager.PERMISSION_GRANTED) {
  
                      Toast.makeText(this,"Permission Granted", Toast.LENGTH_LONG).show();
  
                  } else {
  
                      Toast.makeText(this,"Permission Canceled", Toast.LENGTH_LONG).show();
  
                  }
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance for any help..</p>