Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. import java.io.File;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.HttpVersion;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.FileEntity;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.params.CoreProtocolPNames;
  10. import org.apache.http.util.EntityUtils;
  11.  
  12.  
  13. public class PostFile {
  14. public static void main(String[] args) throws Exception {
  15. HttpClient httpclient = new DefaultHttpClient();
  16. httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  17.  
  18. HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
  19. File file = new File("c:/TRASH/zaba_1.jpg");
  20.  
  21. FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
  22.  
  23. httppost.setEntity(reqEntity);
  24. reqEntity.setContentType("binary/octet-stream");
  25. System.out.println("executing request " + httppost.getRequestLine());
  26. HttpResponse response = httpclient.execute(httppost);
  27. HttpEntity resEntity = response.getEntity();
  28.  
  29. System.out.println(response.getStatusLine());
  30. if (resEntity != null) {
  31. System.out.println(EntityUtils.toString(resEntity));
  32. }
  33. if (resEntity != null) {
  34. resEntity.consumeContent();
  35. }
  36.  
  37. httpclient.getConnectionManager().shutdown();
  38. }
  39. }
  40.  
  41. <?php
  42. if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  43. echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.n";
  44. move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']);
  45. } else {
  46. echo "Possible file upload attack: ";
  47. echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
  48. print_r($_FILES);
  49. }
  50. ?>
  51.  
  52. executing request POST http://localhost:9002/upload.php HTTP/1.1
  53.  
  54. is_uploaded_file
  55.  
  56. is_uploaded_file
  57.  
  58. is_uploaded_file
  59.  
  60. import java.io.File;
  61. import org.apache.http.HttpEntity;
  62. import org.apache.http.HttpResponse;
  63. import org.apache.http.HttpVersion;
  64. import org.apache.http.client.HttpClient;
  65. import org.apache.http.client.methods.HttpPost;
  66. import org.apache.http.entity.mime.MultipartEntity;
  67. import org.apache.http.entity.mime.content.ContentBody;
  68. import org.apache.http.entity.mime.content.FileBody;
  69. import org.apache.http.impl.client.DefaultHttpClient;
  70. import org.apache.http.params.CoreProtocolPNames;
  71. import org.apache.http.util.EntityUtils;
  72.  
  73.  
  74. public class PostFile {
  75. public static void main(String[] args) throws Exception {
  76. HttpClient httpclient = new DefaultHttpClient();
  77. httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  78.  
  79. HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");
  80. File file = new File("c:/TRASH/zaba_1.jpg");
  81.  
  82. MultipartEntity mpEntity = new MultipartEntity();
  83. ContentBody cbFile = new FileBody(file, "image/jpeg");
  84. mpEntity.addPart("userfile", cbFile);
  85.  
  86.  
  87. httppost.setEntity(mpEntity);
  88. System.out.println("executing request " + httppost.getRequestLine());
  89. HttpResponse response = httpclient.execute(httppost);
  90. HttpEntity resEntity = response.getEntity();
  91.  
  92. System.out.println(response.getStatusLine());
  93. if (resEntity != null) {
  94. System.out.println(EntityUtils.toString(resEntity));
  95. }
  96. if (resEntity != null) {
  97. resEntity.consumeContent();
  98. }
  99.  
  100. httpclient.getConnectionManager().shutdown();
  101. }
  102. }
  103.  
  104. File file = new File();
  105.  
  106. HttpEntity httpEntity = MultipartEntityBuilder.create()
  107. .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName())
  108. .build();
  109.  
  110. <dependency>
  111. <groupId>org.apache.httpcomponents</groupId>
  112. <artifactId>httpmime</artifactId>
  113. <version>4.3.1</version>
  114. </dependency>
  115.  
  116. FileBody constructor. ContentBody cbFile = new FileBody(file, "image/jpeg", "FILE_NAME");
  117.  
  118. upload_tmp_dir = "c:mypathmytempfolder"
  119.  
  120. <dependency>
  121. <groupId>org.apache.httpcomponents</groupId>
  122. <artifactId>httpmime</artifactId>
  123. <version>4.2.5</version>
  124. </dependency>
  125.  
  126. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  127. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  128. byte[] imageBytes = baos.toByteArray();
  129.  
  130. HttpClient httpclient = new DefaultHttpClient();
  131. HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);
  132.  
  133. String boundary = "-------------" + System.currentTimeMillis();
  134.  
  135. httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
  136.  
  137. ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
  138. StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
  139. StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);
  140.  
  141. HttpEntity entity = MultipartEntityBuilder.create()
  142. .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
  143. .setBoundary(boundary)
  144. .addPart("group", sbGroup)
  145. .addPart("owner", sbOwner)
  146. .addPart("image", bab)
  147. .build();
  148.  
  149. httpPost.setEntity(entity);
  150.  
  151. try {
  152. HttpResponse response = httpclient.execute(httpPost);
  153. ...then reading response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement