Empty image file after uploading to a server - java

I'm trying to upload an image to a server.
Here is the code I wrote in Android to send the image file along with some other parameters to the server:
static String imagePath = "/storage/sdcard0/Pictures/image.jpg";
static String url = "http://example.com/api";
static String user_id = "99401";
public static void executeMultipartPost() throws IOException, ClientProtocolException {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
File file = new File(imagePath);
FileBody fb = new FileBody(file);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("user_id", new StringBody(user_id));
builder.addPart("type", new StringBody("single"));
builder.addPart("userfile", fb);
final HttpEntity entity = builder.build();
httppost.setEntity(entity);
Log.i(TAG, "Executing request: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Log.i(TAG, ""+response.getStatusLine());
if (resEntity != null) {
Log.i(TAG, EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
Everything works fine, except when it uploads to the server, the server receives an empty image. All I can see is a 0 byte image file. I guess something in the client code is not properly set.
The response I got in Logcat after executing the code is this, (from Laravel error Log):
<html><h2>Unhandled Exception</h2>
<h3>Message:</h3>
<pre>copy(): The first argument to copy() function cannot be a directory</pre>
<h3>Location:</h3>
<pre>/home/ked.ai/www/laravel/file.php on line 92</pre>
<h3>Stack Trace:</h3>
<pre>#0 /home/ked.ai/www/laravel/laravel.php(42): Laravel\Error::native(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92)
#1 [internal function]: Laravel\{closure}(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92, Array)
#2 /home/ked.ai/www/laravel/file.php(92): copy('/', '/home/ked.ai/ww...')
#3 /home/ked.ai/www/application/controllers/api2/item.php(190): Laravel\File::copy('/', '/home/ked.ai/ww...')
#4 [internal function]: Api2_Item_Controller->post_new()
#5 /home/ked.ai/www/laravel/routing/controller.php(325): call_user_func_array(Array, Array)
#6 /home/ked.ai/www/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('new', Array)
#7 /home/ked.ai/www/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('new', Array)
#8 /home/ked.ai/www/laravel/routing/route.php(153): Laravel\Routing\Controller::call('api2.item#new', Array)
#9 /home/ked.ai/www/laravel/routing/route.php(124): Laravel\Routing\Route->response()
#10 /home/ked.ai/www/laravel/laravel.php(167): Laravel\Routing\Route->call()
#11 /home/ked.ai/www/public/index.php(34): require('/home/ked.ai/ww...')
#12 {main}</pre></html>
It seems like it passes a directory instead of a file. Any workaround?

I've managed to find the solution.
I only have to change this line
builder.addPart("userfile", fb);
to
builder.addPart("userfile[]", fb);
because on the server side, $_FILES['userfile'] receives array type.
Thank you.

You can use this method to pass the images to php server.
convert the image to base64 string using Base64.encodeBytes(byte_data).
In the server side php code convert that base64 string to image by base64_decode(str).

Its Working code :
public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url_path);
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
// File file= new File("/mnt/sdcard/forest.png");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bab);
reqEntity.addPart("category ", new StringBody("1"));
reqEntity.addPart("user_id ", new StringBody("55"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
String my_response = convertStreamToString(response.getEntity()
.getContent());
Toast.makeText(getApplicationContext(), my_response,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Try it .

Related

Azure Face API not working with local file

I've been trying to send an image from my computer to this API but I only get the following error: {"error":{"code":"InvalidImageSize","message":"Image size is too small."}}
My code is the following.
I have a PostRequestClass with this method:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(false);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
And on my Main is this one:
public class Test {
public static void main(String[] args) throws URISyntaxException {
PostRequest p = new PostRequest(
"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceAttributes=emotion"
);
p.addHeader("Content-Type", "application/octet-stream");
p.addHeader("Ocp-Apim-Subscription-Key", "my-api-key");
p.sendImageRequest("/Users/user/Desktop/image.jpg");
System.out.println(p.getResponseResult());
}
}
I solved it with the following code:
public void sendImageRequest(String imagePath) {
try {
HttpClient httpClient = new DefaultHttpClient();
File file = new File(imagePath);
FileInputStream fileInputStreamReader = new FileInputStream(file);
byte[] bytes = new byte[(int)file.length()];
fileInputStreamReader.read(bytes);
ByteArrayEntity reqEntity = new ByteArrayEntity(bytes, ContentType.APPLICATION_OCTET_STREAM);
request.setEntity(reqEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
this.responseResult = EntityUtils.toString(entity);
}
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
Go to https://azure.microsoft.com/en-us/services/cognitive-services/face/ and click "API reference".
It will take you to Face API reference page https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
The Face API documenatation says "JPEG, PNG, GIF (the first frame), and BMP format are supported. The allowed image file size is from 1KB to 4MB."
Under the heading "Error code and message returned in JSON",
it says, "InValidImageSize" means "The valid image file size should be larger than or equal to 1KB."

In CKAN, I am trying to upload a file using java client.But getting error code "400" but not showing any error log

In CKAN, I am trying to upload a file using java client.But getting error code "400" but not showing any error log. I have done CKAN setup locally on Centos7 system. Please help if any suggestion, thanks :)
protected String MultiPartPost(String path, String data)
throws CKANException {
String body = "";
String CKANrepos = "http://172.21.9.118:5000";
String CKANapiHeader="X-CKAN-API-Key";
String CKANapi = "api key";
//1st part
String generatedFilename=null;
HttpClient httpclient = new DefaultHttpClient();
String filename = "test.txt";
try {
// create new identifier for every file, use time
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyyMMMddHHmmss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String date=dateFormatGmt.format(new Date());
generatedFilename=date +"/"+filename;
HttpGet getRequest = new HttpGet(this.CKANrepos+ "/api/storage/auth/form/"+generatedFilename);
getRequest.setHeader(CKANapiHeader, this.CKANapi);
HttpResponse response = httpclient.execute(getRequest);
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode!=200){
throw new IllegalStateException("File reservation failed, server responded with code: "+statusCode+
"\n\nThe message was: "+body);
}
}catch (IOException ioe) {
System.out.println(ioe);
} finally {
httpclient.getConnectionManager().shutdown();
}
//2nd part
File file = new File("D:\\test.txt");
httpclient = new DefaultHttpClient();
try {
FileBody bin = new FileBody(file,"text/html");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", bin);
reqEntity.addPart("key", new StringBody(generatedFilename));
HttpPost postRequest = new HttpPost(this.CKANrepos+"/storage/upload_handle");
postRequest.setEntity(reqEntity);
postRequest.setHeader(CKANapiHeader, this.CKANapi);
HttpResponse response = httpclient.execute(postRequest);
int statusCode = response.getStatusLine().getStatusCode();
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String line;
while ((line = br.readLine()) != null) {
body += line;
}
if(statusCode!=200){
System.out.println("statusCode ==" +statusCode);
}
}catch (IOException ioe) {
System.out.println(ioe);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
}

How can i multiple picture send with android

hello i need when i click to button "send" i send all selected picture ( i have all this picture in table picture[]
in PHP i do this
<input id="uploadImageAct" type="file" name="uploadImageAct[]" data-max-size="2048" accept="image/x-png, image/gif, image/jpeg" style="visibility: hidden" multiple="multiple">
and in android for just one picture i do this but i don't know how i do for multi picture (I put all my picture in table picture[] )
this solution is for one image for FileBody and me i need multi image in one FileBody
public JSONObject post(String url, ArrayList<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("uploadFile")) {
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} else {
// Normal string data
Charset chars = Charset.forName("UTF-8");
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue(),chars));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity httpEntity = response.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
How can I change this function so that I can do this
thanks :)
You can upload multiple files in one request along with other string parameters in Android.
For that, you have to include 2 libraries into your project build path, apache-mime4j-0.6.jar and httpmime-4.0.1.jar.
private void doFileUpload(){
File file1 = new File(selectedPath1);
File file2 = new File(selectedPath2);
String urlString = "Your server location";
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody(file1);
FileBody bin2 = new FileBody(file2);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("uploadedfile1", bin1);
reqEntity.addPart("uploadedfile2", bin2);
reqEntity.addPart("user", new StringBody("User"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
final String response_str = EntityUtils.toString(resEntity);
if (resEntity != null) {
Log.i("RESPONSE",response_str);
runOnUiThread(new Runnable(){
public void run() {
try {
res.setTextColor(Color.GREEN);
res.setText("n Response from server : n " + response_str);
Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Or simply visit CoderzHeaven, it may help you.

How to Decode String in Java or android?

I get Data from Json in android,date get and save in String Variable.but when use DecodeUrl its error:
Error: java.lang.IllegalArgumentException: Invalid % sequence at 40:
my code:
#SuppressLint("NewApi")
public String JsonReguest(String url) {
String json = "";
String result = "";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-Type", "application/json");
HttpResponse response;
try {
response = httpclient.execute(httpget);
response.setHeader("Content-Type","UTF-8");
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
InputStream stream = new ByteArrayInputStream(result.getBytes("UTF-8"));
result = convertStreamToString(stream);
// String encode_url=URLEncoder.encode(result,"UTF-8");
// String decode_url=URLDecoder.decode(encode_url,"UTF-8");
//result=decode_url;
//String decodedUrl = URLDecoder.decode(result, "UTF-8");
result=URLDecoder.decode(result);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
simple text of json :
{"CategoryID":11,"ParentID":0,"Title":"%u062E%u0648%u062F%u0631%u0648","PicAddress":""},{"CategoryID":16,"ParentID":0,"Title":"%u0627%u0645%u0644%u0627%u0643%20","PicAddress":""}
this line crashed : result=URLDecoder.decode(result);
how to Resolve Problems.
first decode specifing your encoding
String result = URLDecoder.decode(url, "UTF-8");
and then go to http://json.org/, scroll down and choose one of the supported json parsing Java libraries
As Selvin commented %uxxxx is not a standard Url encoded string , so it's obvious to get an error
you have 2 options:
Contact the service provider to fix her url encoded strings and use URLDecoder.decode in your code
write a custom decoder for such strings
P.S. ask your questions more clear to avoid getting negative points

generate byte array from StringBuffer.toString

What I'm trying to do is to generate a byte array from a url.
byte[] data = WebServiceClient.download(url);
The url returns json
public static byte[] download(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
switch (code) {
case 200:
StringBuffer sb = new StringBuffer();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
is.close();
sContent = sb.toString();
break;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sContent.getBytes();
}
This data is used as a parameter for String
String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);
for some reason, I get this error
I/global ( 631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
I think something there must be missing here sContent = sb.toString(); or here return sContent.getBytes(); but I'm not sure though.
1. Consider using Apache commons-io to read the bytes from InputStream
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
is.close();
}
Currently you're unnecessarily converting the bytes to characters and back.
2. Avoid using String.getBytes() without passing the charset as a parameter. Instead use
String s = ...;
s.getBytes("utf-8")
As a whole I'd rewrite you're method like this:
public static byte[] download(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
if(code != 200) {
throw new IOException(code+" response received.");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
try {
return IOUtils.toByteArray(is);
}finally{
IOUtils.closeQuietly(is.close());
}
}

Categories

Resources