Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. private void doFileUpload(String exsistingFileName){
  2. HttpURLConnection conn = null;
  3. DataOutputStream dos = null;
  4. DataInputStream inStream = null;
  5.  
  6. //String exsistingFileName = "/sdcard/six.3gp";
  7. // Is this the place are you doing something wrong.
  8.  
  9. String lineEnd = "rn";
  10. String twoHyphens = "--";
  11. String boundary = "*****";
  12. int bytesRead, bytesAvailable, bufferSize;
  13. byte[] buffer;
  14. int maxBufferSize = 1*1024*1024;
  15. String urlString = "http://192.168.1.5/upload.php";
  16. try
  17. {
  18. Log.e("MediaPlayer","Inside second Method");
  19. FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  20. URL url = new URL(urlString);
  21. conn = (HttpURLConnection) url.openConnection();
  22. conn.setDoInput(true);
  23. // Allow Outputs
  24. conn.setDoOutput(true);
  25. // Don't use a cached copy.
  26. conn.setUseCaches(false);
  27. // Use a post method.
  28. conn.setRequestMethod("POST");
  29. conn.setRequestProperty("Connection", "Keep-Alive");
  30. conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
  31. dos = new DataOutputStream( conn.getOutputStream() );
  32. dos.writeBytes(twoHyphens + boundary + lineEnd);
  33. dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + exsistingFileName +""" + lineEnd);
  34. dos.writeBytes(lineEnd);
  35. Log.e("MediaPlayer","Headers are written");
  36. bytesAvailable = fileInputStream.available();
  37. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  38. buffer = new byte[bufferSize];
  39. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  40. while (bytesRead > 0)
  41. {
  42. dos.write(buffer, 0, bufferSize);
  43. bytesAvailable = fileInputStream.available();
  44. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  45. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  46. }
  47. dos.writeBytes(lineEnd);
  48. dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  49. BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  50. String inputLine;
  51. String LogString = "";
  52.  
  53. while ((inputLine = in.readLine()) != null) {
  54. LogString= LogString + inputLine;
  55. }
  56.  
  57. Log.i(Utils.TAG, LogString);
  58. // close streams
  59. fileInputStream.close();
  60. dos.flush();
  61. dos.close();
  62. }
  63. catch (MalformedURLException ex)
  64. {
  65. Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
  66. }
  67. catch (IOException ioe)
  68. {
  69. Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
  70. }
  71.  
  72. //------------------ read the SERVER RESPONSE
  73. try {
  74. inStream = new DataInputStream ( conn.getInputStream() );
  75. String str;
  76. while (( str = inStream.readLine()) != null)
  77. {
  78. Log.e("MediaPlayer","Server Response"+str);
  79. }
  80. /*while((str = inStream.readLine()) !=null ){
  81.  
  82. }*/
  83. inStream.close();
  84. }
  85. catch (IOException ioex){
  86. Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
  87. }
  88. }
  89.  
  90. FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
  91.  
  92. InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName));
  93.  
  94. private void doFileUpload(Uri fileUri){
  95. // some code
  96. InputStream inputStream = getContentResolver().openInputStream(fileUri);
  97. // more code
  98. dos.writeBytes("Content-Disposition: form-data; name="uploadedfile";filename="" + "some_file_name" +""" + lineEnd);
  99. // the rest of the code
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement