Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.FileEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.CoreProtocolPNames;
- import org.apache.http.util.EntityUtils;
- public class PostFile {
- public static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
- httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
- HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
- File file = new File("c:/TRASH/zaba_1.jpg");
- FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
- httppost.setEntity(reqEntity);
- reqEntity.setContentType("binary/octet-stream");
- System.out.println("executing request " + httppost.getRequestLine());
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity resEntity = response.getEntity();
- System.out.println(response.getStatusLine());
- if (resEntity != null) {
- System.out.println(EntityUtils.toString(resEntity));
- }
- if (resEntity != null) {
- resEntity.consumeContent();
- }
- httpclient.getConnectionManager().shutdown();
- }
- }
- <?php
- if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
- echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.n";
- move_uploaded_file ($_FILES['userfile'] ['tmp_name'], $_FILES['userfile'] ['name']);
- } else {
- echo "Possible file upload attack: ";
- echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
- print_r($_FILES);
- }
- ?>
- executing request POST http://localhost:9002/upload.php HTTP/1.1
- is_uploaded_file
- is_uploaded_file
- is_uploaded_file
- import java.io.File;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.ContentBody;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.CoreProtocolPNames;
- import org.apache.http.util.EntityUtils;
- public class PostFile {
- public static void main(String[] args) throws Exception {
- HttpClient httpclient = new DefaultHttpClient();
- httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
- HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");
- File file = new File("c:/TRASH/zaba_1.jpg");
- MultipartEntity mpEntity = new MultipartEntity();
- ContentBody cbFile = new FileBody(file, "image/jpeg");
- mpEntity.addPart("userfile", cbFile);
- httppost.setEntity(mpEntity);
- System.out.println("executing request " + httppost.getRequestLine());
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity resEntity = response.getEntity();
- System.out.println(response.getStatusLine());
- if (resEntity != null) {
- System.out.println(EntityUtils.toString(resEntity));
- }
- if (resEntity != null) {
- resEntity.consumeContent();
- }
- httpclient.getConnectionManager().shutdown();
- }
- }
- File file = new File();
- HttpEntity httpEntity = MultipartEntityBuilder.create()
- .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName())
- .build();
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpmime</artifactId>
- <version>4.3.1</version>
- </dependency>
- FileBody constructor. ContentBody cbFile = new FileBody(file, "image/jpeg", "FILE_NAME");
- upload_tmp_dir = "c:mypathmytempfolder"
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpmime</artifactId>
- <version>4.2.5</version>
- </dependency>
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- byte[] imageBytes = baos.toByteArray();
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(StaticData.AMBAJE_SERVER_URL + StaticData.AMBAJE_ADD_AMBAJ_TO_GROUP);
- String boundary = "-------------" + System.currentTimeMillis();
- httpPost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
- ByteArrayBody bab = new ByteArrayBody(imageBytes, "pic.png");
- StringBody sbOwner = new StringBody(StaticData.loggedUserId, ContentType.TEXT_PLAIN);
- StringBody sbGroup = new StringBody("group", ContentType.TEXT_PLAIN);
- HttpEntity entity = MultipartEntityBuilder.create()
- .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
- .setBoundary(boundary)
- .addPart("group", sbGroup)
- .addPart("owner", sbOwner)
- .addPart("image", bab)
- .build();
- httpPost.setEntity(entity);
- try {
- HttpResponse response = httpclient.execute(httpPost);
- ...then reading response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement