android/rails multipart upload problem - java

My problem is that I try to upload an image and some text values to an rails server, and the text values end up as files, insted of just param values.
How the post looks on the server
Parameters: {"action"=>"create", "controller"=>"problems",
"problem"=>{"lon"=>#File:/tmp/RackMultipart20100404-598-8pi1vj-0>,
"photos_attributes"=>{"0"=>{"image"=>#File:/tmp/RackMultipart20100404-598-pak6jk-0>}},
"subject"=>#File:/tmp/RackMultipart20100404-598-nje11p-0>,
"category_id"=>#File:/tmp/RackMultipart20100404-598-ijy1oo-0>,
"lat"=>#File:/tmp/RackMultipart20100404-598-1a7140w-0>,
"email"=>#File:/tmp/RackMultipart20100404-598-1b7w6jp-0>}}
part of the android code
try {
File file = new File(Environment.getExternalStorageDirectory(),"FMS_photo.jpg");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://homepage.com/path");
FileBody bin = new FileBody(file);
Charset chars = Charset.forName("UTF-8");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("problem[photos_attributes][0][image]", bin);
reqEntity.addPart("problem[category_id]", new StringBody("17", chars));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.consumeContent();
}
return true;
} catch (Exception ex) {
globalStatus = UPLOAD_ERROR;
serverResponse = "";
return false;
} finally {
}

That's just the way Rails does multipart form data.
For convenience I added this before_filter in my ApplicationController:
def read_and_parse_json_file_params
file_params = params.select { |name,value| value.is_a?(File) || value.is_a?(Tempfile) }
file_params.reject! { |name,file| file.content_type !~ %r{^application/json} }
file_params.each do |name,file|
file.rewind
params[name] = ActiveSupport::JSON.decode(file.read)
end
end
which parses each part into a JSON hash.

Related

Upload image to Spring Server API in Android

i'm traying to upload a image in Android App to my api, but i have this menssage:
"The current request is not a multipart request"
I've this code in my app android:
#Override
protected String doInBackground(String... uri) {
// url where the data will be posted
String postReceiverUrl = "http://...";
Log.v("TEST", "postURL: " + postReceiverUrl);
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
try {
// the URL where the file will be posted
File file = new File(mCurrentPhotoPath);
FileBody fileBody = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
httpPost.setEntity(reqEntity);
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("TEST ", "Response: " + responseStr);
// you can add an if statement here and do other actions based on the response
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
And in my API i've this code:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public
#ResponseBody
String handleFileUpload(#RequestParam(value = "file", required = false) MultipartFile file) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
File file1 = new File("test.jpg");
FileOutputStream fos = new FileOutputStream(file1);
BufferedOutputStream stream =
new BufferedOutputStream(fos);
stream.write(bytes);
stream.close();
System.out.println("The path is: ");
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getPath());
return "You successfully uploaded \"test.jpg\"!";
} catch (Exception e) {
return "You failed to upload test.jpg => " + e.getMessage();
}
} else {
return "You failed to upload test.jpg because the file was empty.";
}
}
How can i do this?
May be the following code help to you to upload the image in server
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(<server api url>);
MultipartEntity entity = new MultipartEntity();
File myFile = new File(<file_path>);
FileBody fileBody = new FileBody(myFile);
entity.addPart("upload_param_name",fileBody);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
HttpEntity r_entity = response.getEntity();
xmlString = EntityUtils.toString(r_entity);
Log.d("SOAP ", "Result : " + xmlString.toString());
Also for this, you have to use the .jar of apache-mine and httpmine in your apps libs folder.
Finally i convert the image in base64 and send to server how a String and in my Server reconvert to image.
Thanks!

Retrieve image sent via post by android in a jsp page

I am uploading an image to the server via the Android, PHP is very simple and works well with move_uploaded_file (), I do the same in java.
I did not find a way that works, someone has a way to indicate?
Below, it is the code I am using to send the image to the server android, as already remarked, because it works perfectly using the move_uploaded_file() php image is saved successfully!
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(urlServer);
Bitmap bitmap = Util.decodeSampledBitmapFromResource(imagePath, 800, 600);
File file = new File(imagePath);
FileOutputStream fOut = new FileOutputStream(file);
try{
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fOut);
fOut.flush();
fOut.close();}
catch (Exception e) {
e.printStackTrace();
}
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
//Log.e("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
// byte [] responseBody = httppost.getMethod().getBytes();
String ret = "";
//Log.e(""+response.getStatusLine());
if (resEntity != null) {
ret = new String(EntityUtils.toString(resEntity));
Log.e("RESPONSE BODY",ret);
//Log.e("",""+EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();

grails receive file via MultipartEntity

I use this httpclient to post a image to my grails as follows. How do I receive the file in grails?
public static String webPost(String method, File data, Context c) throws Exception {
String json = "";
if (isOnline(c) == true) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost ;
try {
httppost = new HttpPost(method);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(data));
httppost.setEntity(entity);
response = httpclient.execute(httppost);
if (response != null) {
HttpEntity r_entity = response.getEntity();
json = EntityUtils.toString(r_entity);
Log.i("ja", json);
}
} catch (Exception e) {
throw new RuntimeException(e.getLocalizedMessage());
} finally {
httpclient = null;
response = null;
httpclient = null;
}
} else {
throw new RuntimeException("No internet connection");
}
return json;
}
My grails:
def image = request.getFile('image')
image.transferTo(new File('c:/p.png') )
Error:
groovy.lang.MissingMethodException: No signature of method: org.apache.catalina.core.ApplicationHttpRequest.getFile() is applicable for argument types: (java.lang.String) values: [image]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getInfo(), recycle()
at mclient.TestController$_closure1.doCall(TestController.groovy:10)
at mclient.TestController$_closure1.doCall(TestController.groovy)
at java.lang.Thread.run(Thread.java:662)

passing InputStream as parameter to web service

I am trying to upload file but i am not doing it through html form. QueryParam and PathParam can't be used. So can anyone tell how to pass stream.
My HttPClient looks like:
try
{
HttpClient httpclient = new DefaultHttpClient();
InputStream stream=new FileInputStream(new File("C:/localstore/ankita/Desert.jpg"));
String url="http://localhost:8080/Cloud/webresources/fileupload";
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e){}
and my web service class looks somewhat like:
#Path("/fileupload")
public class UploadFileService {
#POST
#Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response uploadFile(InputStream in) throws IOException
{
String uploadedFileLocation = "c://filestore/Desert.jpg" ;
// save it
saveToFile(in, uploadedFileLocation);
String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
// save uploaded file to new location
private void saveToFile(InputStream uploadedInputStream,String uploadedFileLocation)
{
try {
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
Can anyone help??
String url="http://localhost:8080/Cloud/webresources/fileupload";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(new File("C:/localstore/ankita/Desert.jpg")), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
How web service will look like?
You can't do it that way. You can't pass a stream in an HTTP request, because streams are not serializable.
The way to do this is to is create an HttpEntity to wrap the stream (e.g. an InputStreamEntity) then attach it to the HttpPOST object using setEntity. Then the POST is sent, the client will read from your stream and send the bytes as the request's "POST data".

How to upload a file using Apache Commons file upload from a servlet?

I need to upload a xml file from server side, where the contents of a file is in a string. How can I make this file content to be uploaded (basically save) on the server?
This is what I am trying, which works fine, If i give a file directly to FileBody but how to trick it to have filecontents going to another servlet as multipart request?
private def createConfiguration(def sessiontoken)
{
def xmlString=""
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(fileParams.create);
//FileBody bin = new FileBody(new File("C:\\Simon\\myxml.xml"));
StringBody st = new StringBody(sessiontoken);
StringBody cfgname = new StringBody(reqParams.c_Cfgname[0]);
StringBody cfgdesc = new StringBody(reqParams.c_Cfgdesc[0]);
StringBody cfgtype = new StringBody(reqParams.c_Cfgtype[0]);
StringBody cfgfile = new StringBody(reqParams.CFGFILE[0]);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("sessiontoken", st);
reqEntity.addPart("cfgname", cfgname);
reqEntity.addPart("cfgdesc", cfgdesc);
reqEntity.addPart("cfgenv", cfgtype);
//reqEntity.addPart("cfgfile", bin);
reqEntity.addPart("cfgfile", cfgfile);
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
//System.out.println(response.getStatusLine());
if (resEntity != null) {
//System.out.println("Response content length: " + resEntity.getContentLength());
xmlString=resEntity.getContent().getText()
}
EntityUtils.consume(resEntity);
} finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
xmlString
}
If I use the above code I get the below Exception
----------------------------------------
Exception while processing your Request.
No result defined for action com.abc.dc.actions.CreateConfiguration and
result input
Update
So now after checking the tomcat logs & the other server side code, I came to know that that internally dc is getting cfgfile and setting it to
public void setCfgfile(File cfgfile)
{
this.cfgfile = cfgfile
}
which gives me
java.lang.NoSuchMethodException: com.abc.dc.actions.CreateConfiguration.setCfgfile([Ljava.lang.String;)
So how can I overload setCfgfile method with public void setCfgfile(String cfgfile) and convert cfgfile into a File object here?
or Even better,
How can I convert this cfgfile string variable into a FileBody object?
Finally here is how I made it to work :)
private def sendRequest(def sessiontoken,def sendUrl)
{
logger.debug("Inside sendRequest to: "+sendUrl)
def xmlString=""
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(sendUrl);
def filename=reqParams.filename
logger.debug("Filename: "+filename)
FileBody bin = new FileBody(writeToFile(filename,reqParams.cfgfile));
StringBody st = new StringBody(sessiontoken);
StringBody cfgid=new StringBody("")
if(reqParams.containsKey('cfgfile')&&reqParams.cfgid!=null)
{
cfgid= new StringBody(reqParams.cfgid);
}
StringBody cfgname = new StringBody(reqParams.cfgname);
StringBody cfgdesc = new StringBody(reqParams.cfgdesc);
StringBody cfgenv = new StringBody(reqParams.cfgenv);
logger.debug("attaching multipart")
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("sessiontoken", st);
reqEntity.addPart("cfgid", cfgid);
reqEntity.addPart("cfgname", cfgname);
reqEntity.addPart("cfgdesc", cfgdesc);
reqEntity.addPart("cfgenv", cfgenv);
reqEntity.addPart("cfgfile", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
xmlString=resEntity.getContent().getText()
}
EntityUtils.consume(resEntity);
} finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
xmlString
}

Categories