Android and IOUtils.toByteArray - android

I write app on android which will send xml file to PHP server. Here is my code:
InputStream is = new FileInputStream(file);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postReq = new HttpPost("http://majkelsoftgames.cba.pl/ser/server.php");
byte[] data = IOUtils.toByteArray(is);
InputStreamBody isb= new InputStreamBody(new ByteArrayInputStream(data), "file");
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("file", isb);
postReq.setEntity(multipartContent);
HttpResponse response = httpClient.execute(postReq);
My problem is that when
byte[] data = IOUtils.toByteArray(is);
is executing I get:
java.lang.NoClassDefFoundError: org.apache.commons.io.IOUtils
I downloaded external commons-io.jar from http://commons.apache.org/io/ and added this jar to the java build path in android project. I really have no idea what I am doing wrong. Do you have any idea how can I fix it?

You added it to the build path, but did you place it in the /libs directory of youur project? That is the only way it will get added to your apk.

Related

How to upload Image and Video file using HttpPost in android

I need to upload Image and Video File with multiple parameters like File Name, Description , Height and Width using HttpPost method.
Thanks,
Suggestion appreciated.
For uploading a file the efficient way is using HttpPost with multipart/form
Multipart/form The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing
Refer this Upload Files from Android to a Website/Http Server using Post
try this code
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//Path of the file to be uploaded
String filepath = params[0];
File file = new File(filepath);
ContentBody cbFile = new FileBody(file, SET_MIMETYPE);//"audio/basic"
try {
mpEntity.addPart(FILE_NAME, cbFile);
post.setEntity(mpEntity);
HttpResponse response1 = client.execute(post);
HttpEntity resEntity = response1.getEntity();
} catch (Exception e) {
e.printStackTrace();
}
or also refer this link
"http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/"
Thanks

File Upload in Android

I am trying to upload a file along with some other text fields from android. I keep getting this error
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
However if I do an upload without the file (i.e. only text fields) it is working.
The file upload works when I do it from a webpage and hit the same servlet.
This is how i'm working for uploading files this may be useful for you and if you have any questions while implementing you can cas me.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uploadUrl);
File file = new File(path);
if (file.exists()) {
FileBody fileBody = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
if (httpPost != null) {
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);

Use Base64OutputStream to Upload String to Server - Android

I have been using this code, but have been running into some memory issues:
// Get the image from the sdcard
Bitmap bm = BitmapFactory.decodeFile("/sdcard/myimage.jpg");
// turn image into byte array output stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 'compress' the jpeg
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
// get byte[] array of the image
byte[] byteArray = baos.toByteArray();
// turn image into base64 string
String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
// and base64 string to 'params' value pair
params.add(new BasicNameValuePair("userfile", encodedImage));
try {
HttpPost request = new HttpPost();
String urlString = "http://www.example.com";
request.setURI(new URI(urlString));
if(params != null) {
request.setEntity(new UrlEncodedFormEntity(params));
HttpClient client = new DefaultHttpClient();
client.execute(request);
} // end if
} // end try
It has been suggested that I should use Base64OutputStream instead of Base64.encodeToString , but I have not been successful in using Base64OutputStream outputting a string that I can upload to the server. Any examples of using Base64OutputStream on an IMAGE would be a great help.
EDIT
To make the answer work, You need to add two files to your Android project: apache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar;
You can download apache-mime4j-dom-0.7.2.jar from http://james.apache.org/download.cgi#Apache_Mime4J - download the binary, unzip it, and find the apache-mime4j-dom-0.7.2.jar file.
Then go to http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.httpcomponents/httpmime/4.1.3 and download httpmime-4.1.3.jar
Then drag both those files into your project in Eclipse. Then in Eclipse, choose Project > Properties. Select the Properties Pop-up, select Java Build Path. Click the "Libraries" tab (Look for Source | Projects | Libraries | Order and Export). Click "Add Jars" and selectapache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar; Then click the "Order and Export" tab. Check apache-mime4j-dom-0.7.2.jar and httpmime-4.1.3.jar; Then close that popup and choose Project > Clean from the Eclipse menu.
If possible, you should not base64encode your files and send them in URL, but use MultiPart file upload instead:
HttpPost post = new HttpPost(URL);
HttpClient client = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
// picture
entity.addPart( "userfile", new FileBody(
new File( MyApp.getContext().getFilesDir(), "userfile.jpg" ),
"image/jpeg")
);
entity.addPart( "blahblah", new StringBody( "blah" )); // string value
post.setEntity( entity );
client.execute( post );

how to remove an error in "import org.apache.http.entity.mime.content.ByteArrayBody;"

I tried to download [httpmime-4.0.1.jar] but still the error is there and -------------------------------
class ImageUploadTask extends AsyncTask {
#Override
protected String doInBackground(Void... unsued) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(getString(R.string.WebServiceURL) + "/cfc/iphonewebservice.cfc?method=uploadPhoto");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("photoId", new StringBody(getIntent()
.getStringExtra("photoId")));
entity.addPart("returnformat", new StringBody("json"));
entity.addPart("uploaded", new ByteArrayBody(data,"myImage.jpg"));
entity.addPart("photoCaption", new StringBody(caption.getText().toString()));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
Add a libs folder in your application then add “httpmime-4.1.3.jar”, after that you need to show this directory to your project properties.
You should add this JAR to build path. If you are using Eclipse just copy it to project folder. Then in Eclipse right mouse click on it, BUILD PATH->ADD TO BUILD PATH

uploading image on web: getting html source code as response

I am trying to upload image on web server, but whenever it tries it is sending me html source code as response and image is not uploaded there. My code is:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
byte[] buffer = byteStream.toByteArray();
ByteArrayBody body = new ByteArrayBody(buffer,"profile_image");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("b#gmail.com.jpg", body);
post.setEntity(entity);
System.out.println("post entity length "+entity.getContentLength());
ResponseHandler handler = new BasicResponseHandler() ;
String response = client.execute(post,handler);
Thanks in advance!!!
Look at this Example http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/
And change YourUrl

Categories

Resources