Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. public int uploadFile(String sourceFileUri) {
  2. String fileName = sourceFileUri;
  3.  
  4. //Log.i("uploadFile", "File Uri : "
  5. // + fileName);
  6.  
  7.  
  8. HttpURLConnection conn = null;
  9. DataOutputStream dos = null;
  10. String lineEnd = "rn";
  11. String twoHyphens = "--";
  12. String boundary = "*****";
  13. int bytesRead, bytesAvailable, bufferSize;
  14. byte[] buffer;
  15. int maxBufferSize = 1 * 1024 * 1024;
  16. File sourceFile = new File(sourceFileUri);
  17.  
  18. if (!sourceFile.isFile()) {
  19.  
  20. dialog.dismiss();
  21.  
  22. Log.e("SubirImagen", "No existe la imagen :" + imagepath);
  23.  
  24. runOnUiThread(new Runnable() {
  25. public void run() {
  26. //messageText.setText("Source File not exist :"+ imagepath);
  27. Toast.makeText(DisplayImgCamActivity.this, "No existe imagen a subir", Toast.LENGTH_LONG).show();
  28. }
  29. });
  30.  
  31. return 0;
  32.  
  33. }
  34. else
  35. {
  36. try {
  37.  
  38. // open a URL connection to the Servlet
  39. FileInputStream fileInputStream = new FileInputStream(sourceFile);
  40. URL url = new URL(urlServer);
  41.  
  42. // Open a HTTP connection to the URL
  43. conn = (HttpURLConnection) url.openConnection();
  44. conn.setDoInput(true); // Allow Inputs
  45. conn.setDoOutput(true); // Allow Outputs
  46. conn.setUseCaches(false); // Don't use a Cached Copy
  47. conn.setRequestMethod("POST");
  48. conn.setRequestProperty("Connection", "Keep-Alive");
  49. conn.setRequestProperty("ENCTYPE", "multipart/form-data");
  50. conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
  51. conn.setRequestProperty("uploaded_file", fileName);
  52.  
  53. dos = new DataOutputStream(conn.getOutputStream());
  54.  
  55. dos.writeBytes(twoHyphens + boundary + lineEnd);
  56. dos.writeBytes("Content-Disposition: form-data; name="uploaded_file";filename=""
  57. + fileName + """ + lineEnd);
  58.  
  59. dos.writeBytes(lineEnd);
  60.  
  61. // create a buffer of maximum size
  62. bytesAvailable = fileInputStream.available();
  63.  
  64. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  65. buffer = new byte[bufferSize];
  66.  
  67. // read file and write it into form...
  68. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  69.  
  70. while (bytesRead > 0) {
  71.  
  72. dos.write(buffer, 0, bufferSize);
  73. bytesAvailable = fileInputStream.available();
  74. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  75. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  76.  
  77. }
  78.  
  79. // send multipart form data necesssary after file data...
  80. dos.writeBytes(lineEnd);
  81. dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  82.  
  83. // Responses from the server (code and message)
  84. serverResponseCode = conn.getResponseCode();
  85. String serverResponseMessage = conn.getResponseMessage();
  86.  
  87. Log.i("uploadFile", "HTTP Response is : "
  88. + serverResponseMessage + ": " + serverResponseCode);
  89.  
  90. if(serverResponseCode == 200){
  91.  
  92. runOnUiThread(new Runnable() {
  93. public void run() {
  94. String msg = "File Upload Completed.nn See uploaded file here : nn"
  95. +" F:/wamp/wamp/www/uploads";
  96. //messageText.setText(msg);
  97. Toast.makeText(DisplayImgCamActivity.this, "Imagen subida satisfactoriamente", Toast.LENGTH_LONG).show();
  98. }
  99. });
  100. }
  101.  
  102. //close the streams //
  103. fileInputStream.close();
  104. dos.flush();
  105. dos.close();
  106.  
  107. } catch (MalformedURLException ex) {
  108.  
  109. dialog.dismiss();
  110. ex.printStackTrace();
  111.  
  112. runOnUiThread(new Runnable() {
  113. public void run() {
  114. //messageText.setText("MalformedURLException Exception : check script url.");
  115. Toast.makeText(DisplayImgCamActivity.this, "MalformedURLException", Toast.LENGTH_LONG).show();
  116. }
  117. });
  118.  
  119. Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
  120. } catch (Exception e) {
  121.  
  122. dialog.dismiss();
  123. e.printStackTrace();
  124.  
  125. runOnUiThread(new Runnable() {
  126. public void run() {
  127. //messageText.setText("Got Exception : see logcat ");
  128. Toast.makeText(DisplayImgCamActivity.this, "Error: ver logcat ", Toast.LENGTH_LONG).show();
  129. }
  130. });
  131. Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
  132. }
  133. dialog.dismiss();
  134. return serverResponseCode;
  135.  
  136. } // End else block
  137. }
  138.  
  139. public Uri getImageUri(Context inContext, Bitmap inImage) {
  140. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  141. inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  142. String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  143. return Uri.parse(path);
  144. }
  145.  
  146. public String getRealPathFromURI(Uri uri) {
  147. Cursor cursor = getContentResolver().query(uri, null, null, null, null);
  148. cursor.moveToFirst();
  149. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  150. return cursor.getString(idx);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement