how to httpput the image on a remote server from android - java

I have tried with following code , but its not working
File file= new File(filename);
byte[] data = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://www.example.com/resource");
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);
for (int i = 0; i pairs = new ArrayList();
pairs.add(new BasicNameValuePair("Data", data));
put.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(put);
I could not able to PUT the data on the server I have also tried with HttpsURLConnection but its getting uploaded.

After setting the content length I am able to do HTTP put , with the help of HttpURLConnection as follows https.setRequestProperty("Content-Type", "image/jpeg");
https.setRequestProperty("Content-Length", "" + file.length());

Related

Servlet request.getParameter() always return null value

I'm working on simple client server communication using HttpPost. From the client side I'm setting a parameter(filename).
At the server side when I try to get the parameter value it's always showing null. I tried using MultiPartEntity but even that is not working.
Below is my client code:
HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx:yyyy");
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(dataFile), -1);
reqEntity.setContentType("binary/octet-stream");
// Send in multiple parts if needed
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
//setting the parameter
httppost.getParams().setParameter("filename", "xxxx.xml");
HttpResponse response = httpclient.execute(httppost);
int respcode = response.getStatusLine().getStatusCode();
And this is my servlet code:
response.setContentType("binary/octet-stream");
Scanner scanner = new Scanner(request.getInputStream());
// reading the parameter
String filename = request.getParameter("filename");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\" + filename)));
Kindly let me know any possible solution for this issue.
Thanks in advance!
Ur setting parameters wrong... at the client side, do this:
ArrayList<NameValuePair> postParameters = postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("filename", "xxxx.xml");
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);

Send Byte array as file in using http client in java

We have byte array of file and we want to upload it as file.
FileBody only gets File as parameter but we have a array of bytes.
One solution is to save byte array into file and then send it
but it is not appropriate for me.
byte b[]= new byte[1000];
//fill b
MultipartEntity form = new MultipartEntity();
form.addPart("file", new FileBody(/* b? */));
thanks.
You can do something like
HttpClient client=null;
byte b[]= new byte[1000];
MultipartEntity form = new MultipartEntity();
ContentBody cd = new InputStreamBody(new ByteArrayInputStream(b), "my-file.txt");
form.addPart("file", cd);
HttpEntityEnclosingRequestBase post = new HttpPost("");//If a PUT request then `new HttpPut("");`
post.setEntity(form);
client.execute(post);
You can use ByteArrayBody instead InputStreamBody or FileBody.
HttpClient client=null;
byte b[]= new byte[1000];
MultipartEntity form = new MultipartEntity();
ContentBody cd = new ByteArrayBody(b, "my-file.txt");
form.addPart("file", cd);
HttpEntityEnclosingRequestBase post = new HttpPost("");
post.setEntity(form);
client.execute(post);

Multipart, set Content-Type of one part

I have this code to post data to my server:
// HTTP Settings
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://myserver.com/Login");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
// Http Headers
postRequest.addHeader("Accept", "application/xml");
postRequest.addHeader("Connection", "keep-alive");
// Credentials
reqEntity.addPart("username", new StringBody(ServerData.username));
reqEntity.addPart("password", new StringBody(ServerData.password));
if (m_sigFile.exists()) {
Bitmap m_sig = BitmapFactory.decodeFile(sigFilePath
+ "m_sig.jpg");
ByteArrayOutputStream m_bao = new ByteArrayOutputStream();
m_sig.compress(Bitmap.CompressFormat.JPEG, 90, m_bao);
byte[] m_ba = m_bao.toByteArray();
String m_ba1 = Base64.encodeToString(m_ba, 0);
reqEntity.addPart("m_sig.jpg", new StringBody(m_ba1));
}
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
The code works perfectly, all data is send to the server except for the jpeg file. The server only accepts the file if I set the content type to 'image/jpeg', but only for the image. The username and password has to be in plain text. Is this possible?
This will work:
ContentBody cbFile = new FileBody(new File(myPath
+ "image_1.jpg"),
"image/jpeg");
reqEntity.addPart("photo1"), cbFile);
Don't forget to check if you file exists!
There is a constructor for StringBody that accepts content type:
new StringBody(titleString, "application/atom+xml", Charset.forName("UTF-8"));

Upload image byte[] with POST

How to send Image or Video with post request with lot off other parameters with post request ? I have image and video in byte[] format and I am trying like
HttpClient httpClient = new DefaultHttpClient();
http.setURI(URI.create(url));
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody bab = new ByteArrayBody(a.getData(),
a.getQuestionId() + ".jpg");
reqEntity.addPart("type", new StringBody("photo"));
reqEntity.addPart("data", bab);
http.setEntity(reqEntity);
HttpResponse response = httpClient.execute(http);
it pass through code but it doesn't upload anything ( bab !=null ). Does anybody have any idea what is wrong, I cannot provide more info, other team is at ws ?
Your code should look something more like this:
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody bab = new ByteArrayBody(a.getData(), a.getQuestionId() + ".jpg");
reqEntity.addPart("type", new StringBody("photo"));
reqEntity.addPart("data", bab);
HttpPost postMethod = new HttpPost(url);
postMethod.setEntity(reqEntity);
HttpClient httpClient = new DefaultHttpClient():
HttpResponse response = httpClient.execute(postMethod);

GZIP in Android

i just wanted to ask about sending gzip for post requests using HttpClient in Android?
where to get that OutputStream to be passed in the GZIPOutputstream?
any snippets?
Hi UseHttpUriRequest as shown below
String urlval=" http"//www.sampleurl.com/";
HttpUriRequest req = new HttpGet(urlval);
req.addHeader("Accept-Encoding", "gzip");
httpClient.execute(req);
and then Check response for content encoding as shown below :
InputStream is = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
is = new GZIPInputStream(is);
}
If your data is not too large, you can do it like this:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(POST_URL);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(data.getBytes());
gos.close();
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(baos.toByteArray());
httpost.setEntity(byteArrayEntity);

Categories

Resources