Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. public String postFile(String fileName) throws Exception {
  2.  
  3. fileName = "/mnt/sdcard/DCIM/100MEDIA/IMAG0309.jpg";
  4.  
  5. HttpClient client = new DefaultHttpClient();
  6. HttpPost post = new HttpPost("http://www.mywebsite.com/upload_file.php");
  7. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  8. builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  9.  
  10. final File file = new File(fileName);
  11. FileBody fb = new FileBody(file);
  12. builder.addPart("file",fb);
  13.  
  14. String userid = session_userid;
  15. builder.addTextBody("userID", userid);
  16.  
  17. final HttpEntity yourEntity = builder.build();
  18.  
  19. class ProgressiveEntity implements HttpEntity {
  20. @Override
  21. public void consumeContent() throws IOException {
  22. yourEntity.consumeContent();
  23. }
  24. @Override
  25. public InputStream getContent() throws IOException,
  26. IllegalStateException {
  27. return yourEntity.getContent();
  28. }
  29. @Override
  30. public Header getContentEncoding() {
  31. return yourEntity.getContentEncoding();
  32. }
  33. @Override
  34. public long getContentLength() {
  35. return yourEntity.getContentLength();
  36. }
  37. @Override
  38. public Header getContentType() {
  39. return yourEntity.getContentType();
  40. }
  41. @Override
  42. public boolean isChunked() {
  43. return yourEntity.isChunked();
  44. }
  45. @Override
  46. public boolean isRepeatable() {
  47. return yourEntity.isRepeatable();
  48. }
  49. @Override
  50. public boolean isStreaming() {
  51. return yourEntity.isStreaming();
  52. } // CONSIDER put a _real_ delegator into here!
  53.  
  54. @Override
  55. public void writeTo(OutputStream outstream) throws IOException {
  56.  
  57. class ProxyOutputStream extends FilterOutputStream {
  58.  
  59. public ProxyOutputStream(OutputStream proxy) {
  60. super(proxy);
  61. }
  62. public void write(int idx) throws IOException {
  63. out.write(idx);
  64. }
  65. public void write(byte[] bts) throws IOException {
  66. out.write(bts);
  67. }
  68. public void write(byte[] bts, int st, int end) throws IOException {
  69. out.write(bts, st, end);
  70. }
  71. public void flush() throws IOException {
  72. out.flush();
  73. }
  74. public void close() throws IOException {
  75. out.close();
  76. }
  77. }
  78.  
  79. class ProgressiveOutputStream extends ProxyOutputStream {
  80.  
  81. int totalSent;
  82.  
  83. public ProgressiveOutputStream(OutputStream proxy) {
  84. super(proxy);
  85. }
  86. public void write(byte[] bts, int st, int end) throws IOException {
  87.  
  88. // FIXME Put your progress bar stuff here!
  89.  
  90. out.write(bts, st, end);
  91. }
  92. }
  93.  
  94. yourEntity.writeTo(new ProgressiveOutputStream(outstream));
  95. }
  96.  
  97. };
  98. ProgressiveEntity myEntity = new ProgressiveEntity();
  99.  
  100. post.setEntity(myEntity);
  101. HttpResponse response = client.execute(post);
  102.  
  103. return getContent(response);
  104.  
  105. }
  106.  
  107. @Override
  108. protected String doInBackground(HttpResponse... arg0)
  109. {
  110. String fileName = "/mnt/sdcard/DCIM/100MEDIA/IMAG0309.jpg";
  111. HttpClient client = new DefaultHttpClient();
  112. HttpPost post = new HttpPost("http://www.mywebsite.com/upload_file.php");
  113. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  114. builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
  115.  
  116. final File file = new File(fileName);
  117. FileBody fb = new FileBody(file);
  118. builder.addPart("file",fb);
  119.  
  120. String userid = session_userid;
  121. builder.addTextBody("userID", userid);
  122. final HttpEntity yourEntity = builder.build();
  123.  
  124. class ProgressiveEntity implements HttpEntity {
  125. @Override
  126. public void consumeContent() throws IOException {
  127. yourEntity.consumeContent();
  128. }
  129. @Override
  130. public InputStream getContent() throws IOException,
  131. IllegalStateException {
  132. return yourEntity.getContent();
  133. }
  134. @Override
  135. public Header getContentEncoding() {
  136. return yourEntity.getContentEncoding();
  137. }
  138. @Override
  139. public long getContentLength() {
  140. return yourEntity.getContentLength();
  141. }
  142. @Override
  143. public Header getContentType() {
  144. return yourEntity.getContentType();
  145. }
  146. @Override
  147. public boolean isChunked() {
  148. return yourEntity.isChunked();
  149. }
  150. @Override
  151. public boolean isRepeatable() {
  152. return yourEntity.isRepeatable();
  153. }
  154. @Override
  155. public boolean isStreaming() {
  156. return yourEntity.isStreaming();
  157. } // CONSIDER put a _real_ delegator into here!
  158.  
  159. @Override
  160. public void writeTo(OutputStream outstream) throws IOException {
  161.  
  162. class ProxyOutputStream extends FilterOutputStream {
  163.  
  164. public ProxyOutputStream(OutputStream proxy) {
  165. super(proxy);
  166. }
  167. public void write(int idx) throws IOException {
  168. out.write(idx);
  169. }
  170. public void write(byte[] bts) throws IOException {
  171. out.write(bts);
  172. }
  173. public void write(byte[] bts, int st, int end) throws IOException {
  174. out.write(bts, st, end);
  175. }
  176. public void flush() throws IOException {
  177. out.flush();
  178. }
  179. public void close() throws IOException {
  180. out.close();
  181. }
  182. }
  183.  
  184. class ProgressiveOutputStream extends ProxyOutputStream {
  185. public ProgressiveOutputStream(OutputStream proxy) {
  186. super(proxy);
  187. int totalSent = 0;
  188. }
  189. public void write(byte[] bts, int st, int end) throws IOException {
  190.  
  191. // FIXME Put your progress bar stuff here!
  192. //what's totalSize?
  193. publishProgress((int) ((end / (float) totalSize) * 100));
  194. out.write(bts, st, end);
  195. }
  196. }
  197.  
  198. yourEntity.writeTo(new ProgressiveOutputStream(outstream));
  199. }
  200.  
  201. };
  202. ProgressiveEntity myEntity = new ProgressiveEntity();
  203.  
  204. post.setEntity(myEntity);
  205. HttpResponse response = client.execute(post);
  206.  
  207. return getContent(response);
  208. //Here it says I should put this into a try catch but then I get an error on
  209. // the doInBackground line saying it needs to return a string
  210.  
  211. }
  212.  
  213. protected void onProgressUpdate(Integer... progress) {
  214. pd.setProgress((int) (progress[0]));
  215. }
  216.  
  217. @SuppressWarnings("deprecation")
  218. @Override
  219. protected void onPostExecute(String file_url) {
  220. pd.dismiss();
  221. connection.disconnect();
  222. Toast.makeText(getApplicationContext(), "Ready.", Toast.LENGTH_LONG).show();
  223. }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement