Upload Photo Using Httppost Multipartentitybuilder

Upload Photo using HttpPost MultiPartEntityBuilder

Sample Image
Check that jar in Order and Export tab and run.

Post files to server from Android - MultipartEntityBuilder - HTTP

I was fortunate enough to find one Answer by Krystian in Stackoverflow that solved my issue.

After adding the boundary it started working. I had to add it to the HTTP Post

String boundary = "-------------" + System.currentTimeMillis();
httppost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);

and to the entity

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setBoundary(boundary);

Now I get everything in the server:

10-06 13:16:06.977: I/UploadFileAsync(31506): RESPONSE FROM SERVER: Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506): [flies] => yes
10-06 13:16:06.977: I/UploadFileAsync(31506): [eats] => no
10-06 13:16:06.977: I/UploadFileAsync(31506): [friend] => yes
10-06 13:16:06.977: I/UploadFileAsync(31506): )
10-06 13:16:06.977: I/UploadFileAsync(31506): Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506): [randomvar] => 42
10-06 13:16:06.977: I/UploadFileAsync(31506): [mystr] => blaka
10-06 13:16:06.977: I/UploadFileAsync(31506): )
10-06 13:16:06.977: I/UploadFileAsync(31506): Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506): [file] => Array
10-06 13:16:06.977: I/UploadFileAsync(31506): (
10-06 13:16:06.977: I/UploadFileAsync(31506): [name] => serverXML.xml
10-06 13:16:06.977: I/UploadFileAsync(31506): [type] => application/octet-stream
10-06 13:16:06.977: I/UploadFileAsync(31506): [tmp_name] => /tmp/phpdmEsn2
10-06 13:16:06.977: I/UploadFileAsync(31506): [error] => 0
10-06 13:16:06.977: I/UploadFileAsync(31506): [size] => 3548
10-06 13:16:06.977: I/UploadFileAsync(31506): )
10-06 13:16:06.977: I/UploadFileAsync(31506): )

Upload file by HTTP POST

You seem to be missing a dependency.

I have httpclient-4.2.5, httpcleint-cache-4.2.5, httpcore-4.2.4 and httpmime-4.2.5 with commons-lang3-3.1 and commons-logging-1.1.2 jars in my class path.

I also use entity.addPart(formFieldName, new FileBody(file, "image/jpeg")); instead of using the header property.

Take a look at Internet media type for more details

Upload a file through an HTTP form, via MultipartEntityBuilder, with a progress bar

The winning code (in spectacular Java-Heresy(tm) style) is:

public static String postFile(String fileName, String userName, String password, String macAddress) throws Exception {

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER + "uploadFile");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

final File file = new File(fileName);
FileBody fb = new FileBody(file);

builder.addPart("file", fb);
builder.addTextBody("userName", userName);
builder.addTextBody("password", password);
builder.addTextBody("macAddress", macAddress);
final HttpEntity yourEntity = builder.build();

class ProgressiveEntity implements HttpEntity {
@Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
@Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
@Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
@Override
public long getContentLength() {
return yourEntity.getContentLength();
}
@Override
public Header getContentType() {
return yourEntity.getContentType();
}
@Override
public boolean isChunked() {
return yourEntity.isChunked();
}
@Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
@Override
public boolean isStreaming() {
return yourEntity.isStreaming();
} // CONSIDER put a _real_ delegator into here!

@Override
public void writeTo(OutputStream outstream) throws IOException {

class ProxyOutputStream extends FilterOutputStream {
/**
* @author Stephen Colebourne
*/

public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
} // CONSIDER import this class (and risk more Jar File Hell)

class ProgressiveOutputStream extends ProxyOutputStream {
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(byte[] bts, int st, int end) throws IOException {

// FIXME Put your progress bar stuff here!

out.write(bts, st, end);
}
}

yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}

};
ProgressiveEntity myEntity = new ProgressiveEntity();

post.setEntity(myEntity);
HttpResponse response = client.execute(post);

return getContent(response);

}

public static String getContent(HttpResponse response) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
String content = "";

while ((body = rd.readLine()) != null)
{
content += body + "\n";
}
return content.trim();
}

# NOTE ADDED LATER: as this blasterpiece gets copied into various code lineages,
# The management reminds the peanut gallery that "Java-Heresy" crack was there
# for a reason, and (as commented) most of that stuff can be farmed out to off-
# the-shelf jar files and what-not. That's for the java lifers to tool up. This
# pristine hack shall remain obviousized for education, and for use in a pinch.

# What are the odds??

Android : upload Image and JSON using MultiPartEntityBuilder

To send binary data you need to use addBinaryBody method of MultipartEntityBuilder. Sample of attaching:

import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
//Image attaching
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
File file;
multipartEntity.addBinaryBody("someName", file, ContentType.create("image/jpeg"), file.getName());
//Json string attaching
String json;
multipartEntity.addPart("someName", new StringBody(json, ContentType.TEXT_PLAIN));

Then make request as usual:

HttpPut put = new HttpPut("url");
put.setEntity(multipartEntity.build());
HttpResponse response = client.execute(put);
int statusCode = response.getStatusLine().getStatusCode();


Related Topics



Leave a reply



Submit