Upload large images in background


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. int maxBufferSize = 1*256*1024;
  2. int headerSize=89;
  3. String urlString = "http://xxxxxxxx.com/upload.php";
  4. int fileSize=0;
  5. int infoSize=0;
  6.  
  7. protected Integer doInBackground(String... file) {
  8. Integer ret=0;
  9.  
  10. HttpURLConnection conn = null;
  11. DataOutputStream dos = null;
  12. DataInputStream inStream = null;
  13.  
  14. String exsistingFileName = file[0];
  15.  
  16. String lineEnd = "
  17. ";
  18. String twoHyphens = "--";
  19. String boundary = "*****";
  20.  
  21. int bytesRead, bytesAvailable, bufferSize;
  22. byte[] buffer;
  23.  
  24. try
  25. {
  26. Log.i("MyAPP","Start uploading file: " + file[0]);
  27.  
  28. FileInputStream fileInputStream = new FileInputStream(
  29. new File(exsistingFileName));
  30.  
  31. // open a URL connection to the Servlet
  32. URL url = new URL(urlString);
  33.  
  34. //Open a HTTP connection to the URL
  35. conn = (HttpURLConnection) url.openConnection();
  36.  
  37. // Allow Inputs
  38. conn.setDoInput(true);
  39.  
  40. // Allow Outputs
  41. conn.setDoOutput(true);
  42.  
  43. // Don't use a cached copy.
  44. conn.setUseCaches(false);
  45.  
  46. // Use a post method.
  47. fileSize=fileInputStream.available();
  48. infoSize=fileSize + headerSize + file[0].length();
  49. conn.setFixedLengthStreamingMode(infoSize);
  50. conn.setRequestMethod("POST");
  51.  
  52. conn.setRequestProperty("Connection", "Keep-Alive");
  53. conn.setRequestProperty("Content-Type",
  54. "multipart/form-data;boundary="+boundary);
  55.  
  56. dos = new DataOutputStream( conn.getOutputStream() );
  57.  
  58. dos.writeBytes(twoHyphens + boundary + lineEnd);
  59. dos.writeBytes("Content-Disposition: form-data;
  60. name='uploadedfile';filename='" + file[0] + "'" + lineEnd);
  61. dos.writeBytes(lineEnd);
  62.  
  63. // create a buffer of maximum size
  64. bytesAvailable = fileInputStream.available();
  65. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  66. buffer = new byte[bufferSize];
  67.  
  68. // read file and write it into form...
  69. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  70.  
  71. int bytesSent=0;
  72. while (bytesRead > 0)
  73. {
  74. if (bytesSent > 0){
  75. int pg=(bytesSent*100)/infoSize;
  76. publishProgress(pg);
  77. }
  78. bytesSent+=bufferSize;
  79. dos.write(buffer, 0, bufferSize);
  80. bytesAvailable = fileInputStream.available();
  81. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  82. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  83. }
  84.  
  85. // send multipart form data necesssary after file data...
  86. dos.writeBytes(lineEnd);
  87. dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  88.  
  89. //close streams
  90. fileInputStream.close();
  91. dos.flush();
  92. dos.close();
  93.  
  94. }
  95. {
  96. Log.e("MyAPP", "error: " + ex.getMessage(), ex);
  97. ret=1;
  98. }
  99. catch (IOException ioe)
  100. {
  101. Log.e("MyAPP", "error: " + ioe.getMessage(), ioe);
  102. ret=1;
  103. }
  104.  
  105. try {
  106. inStream = new DataInputStream ( conn.getInputStream() );
  107. String str;
  108.  
  109. while (( str = inStream.readLine()) != null)
  110. {
  111. Log.i("MyAPP","Server Response"+str);
  112. }
  113. inStream.close();
  114. }
  115. catch (IOException ioex){
  116. Log.e("MyAPP", "error: " + ioex.getMessage(), ioex);
  117. ret=1;
  118. }
  119.  
  120. return ret;
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.