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);
Related
how to upload bin file from internal storage to http server in android?
I'm getting permission denied error.
String url = "url_path";
String PATH = "/storage/emulated/0/data/";
Log.v("response path ", "PATH: " + PATH);
File file = new File(PATH,
"/test.bin");
if(file.exists()){
Log.d("response : "," file available");
} else {
Log.d("response : "," file not available");
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
Error Log:
java.io.FileNotFoundException: /storage/emulated/0/data/test.bin (Permission denied)
You have permissions for your App's data directory only. unless you have rooted android device.
or
A System app.
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
I am creating an android application for taking photos and videos. After capture images I want to send this image with date and some text to web server. In server side I am making an application with this pictures and videos. The image captured will be saved in memory card. How can I send image with text using JSON. Also I want to send Videos to the web server.
You can do this with a Multipart post request:(This way, you dont need to create json)
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverURL);
MultipartEntity postEntity = new MultipartEntity();
File file = new File("Your File path on SD card");
postEntity.addPart("fileupload", new FileBody(file, "image/jpeg"));
postEntity.addPart("loginKey", new StringBody(""+loginKey));
postEntity.addPart("message", new StringBody(message));
postEntity.addPart("token", new StringBody(token));
post.setEntity(postEntity);
response = client.execute(post);
You have to add this mime4j library.
try this to uplaod text ,image to server in asynctask
FileBody filebodyVideo = new FileBody(new File(
"Sd Card VideoPath"));
FileBody filebodyVideo1 = new FileBody(new File("Video Upload url"));
StringBody Title= new StringBody("Put Title Here");
StringBody description= new StringBody("Put Desc Here");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("image", filebodyVideo);
multipartContent.addPart("Title", Title);
multipartContent.addPart("Description", description);
httppost.setEntity(multipartContent);
You can use this code in your asynctask:
File file = new File("Your File path on SD card");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("YourUrl");
MultipartEntity entity = new MultipartEntity();
entity.addPart("YourKey",new StringBody("Your Text"));
entity.addPart("File", new FileBody(file));
httpost.setEntity(entity);
HttpResponse response = httpclient.execute(httpost);
I wrote the code to record a video on android, I upload it to the Blobstore, but no luck in getting it play either from the player on googleappengine or stream it on a web application however, I can read the video when I download it. Any idea ?? I thought of the encodings I am using or the video format(.mp4) or even more, the way i send the bytes to the blobstore. thank you.
here is the recording code:
mCamera = Camera.open();
path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Date date=new Date();
filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
//create empty file it must use
File file=new File(path,filename);
mrec = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mrec.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mrec.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoFrameRate(20);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(path+filename);
mrec.prepare();
mrec.start();
and here is the uploading code:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
//HttpPost httppost = new HttpPost(url);
HttpPost httppost = new HttpPost(url);
File file = new File("/sdcard/"+videoName);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "video/mp4");
mpEntity.addPart("videoFile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
the "url" parameter is the generated url from the blobstore in order to upload the media file, i guess blobstore users are familiar with it.
Try using OutputFormat.MPEG_4, VideoEncoder.H264 and AudioEncoder.AAC.
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.