I'm working on a project that needs to upload image using Multipart.
Here is my code:
public void doUploadGIF(File image) {
try {
long start = System.currentTimeMillis();
HttpPost httppost = new HttpPost(
"myurl");
MultipartEntity multipartEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("gif", new FileBody(image));
httppost.setEntity(multipartEntity);
mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
long end = System.currentTimeMillis();
System.out.println("Time for uploading:"
+ String.valueOf(end - start));
} catch (Exception e) {
Log.e("Upload", "Error while uploading");
}
}
private class PhotoUploadResponseHandler implements ResponseHandler<Object> {
#Override
public Object handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity r_entity = response.getEntity();
String responseString = EntityUtils.toString(r_entity);
Log.d("UPLOAD", responseString);
return null;
}
}
Now in client side (Android) I let the user know the process of uploading ( percent ).
How can I do that?
Thanks for your attention !
in my case I used the file size in KB and set the limit of the progressbar with the file size in KB. and for updating the progressbar I used :
int bufferLength = 1024;
File file = new File(ImgURI);
mFileLen = file.length();
int maxBufferSize = 1*1024*1024;
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
for (int i = 0; i < bufferSize; i +=bufferLength) {
progress = i/1024;
progressBar1.setMax(kbfilesize);
publishProgress(progress);
}
Related
I take a picture using the android.hardware.Camera API. I then convert it to a Bitmap of half the actual size, compress it to a JPEG of quality 80, convert it to Base64 and send it to the server as follows.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP);
String json_response = "";
try {
URL url = new URL("https://example.com/api_endpoint");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write("?reg=" + regCode);
writer.write("&img=" + encoded);
writer.flush();
writer.close();
os.close();
Log.d("Auth", conn.getResponseCode() + "");
InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(in);
String text = "";
while ((text = br.readLine()) != null) {
json_response += text;
}
conn.disconnect();
} catch (IOException e) {
Log.d(getClass().getName(), "" + e.getMessage());
}
This works as expected. Now, If I don't resize the image and keep the quality 100%, how should I go about to avoid an OutOfMemoryError? My application requires the image to be in the full resolution and best quality possible.
My questions are:
Is the way I am uploading the correct way?
How to send Image is best quality without OutOfMemoryError i.e. how to optimize RAM usage in this process?
Here is my image/file uploader class:
public class ImageUploader extends AsyncTask<String, String, String> {
File imageFile = null;
String fileName = null;
public ImageUploader(File imageFile, String fileName){
this.imageFile = imageFile;
this.fileName = fileName;
}
#Override
protected String doInBackground(String... params) {
String url_str = params[0];
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="fSnd";
try {
URL url = new URL(url_str);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("POST");
c.setDoInput(true);
c.setDoOutput(true);
c.setRequestProperty("Connection", "Keep-Alive");
c.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
c.connect();
DataOutputStream dos = new DataOutputStream(c.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + this.fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
FileInputStream fin = new FileInputStream(imageFile);
int bytesAvailable = fin.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[ ] buffer = new byte[bufferSize];
int bytesRead = fin.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fin.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fin.read(buffer, 0,bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fin.close();
dos.flush();
dos.close();
StringBuilder response = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
}
return null;
}
}
Usage:
new ImageUploader(pictureFile, "sample.jpg"){
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}.execute("http://example/upload.php");
PHP:
<?php
$file = explode('.', $_FILES['file']['name']);
$ext = $file[count($file) - 1];
$name = substr($_FILES['file']['name'], 0, (strlen($ext) + 1) * -1);
$location = 'images/';
$cntr = 1;
$tmp_name = $name;
if(move_uploaded_file($_FILES['file']['tmp_name'], $location.$tmp_name.'.'.$ext)){
echo "Image was uploaded.";
}else{
echo "Image was not uploaded.";
}
?>
If you have the control over the API endpoint. Then try to implement the POST request to accept multi-part uploading from client side.
On client-side, have something like this to upload the image to API (with Okhttp client)
private static final String IMGUR_CLIENT_ID = "...";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("title", "Square Logo")
.addFormDataPart("image", "logo-square.png",
RequestBody.create(MEDIA_TYPE_PNG, new File("website/static/logo-square.png")))
.build();
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.url("https://api.imgur.com/3/image")
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
I think problem not with downloading to server. If I understand correctly, you getting image from camera and sending it. Note, that if you using simple request intent, that returns in onActivityResult() - Bitmap Image - this may be point of OutOfMemoryException...
Solution it's use another form on Intent() method, (that can get storage path in his parameters) for getting photo from camera, that doesn't return Bitmap image. But save photo to path, which you specified. And now you can do anything with photo in path, without OutOfMemoryException...
Sample starting correct Intent:
File destination = new File(Environment.getExternalStorageDirectory(),
"image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(destination));
startActivityForResult(intent, CAMERA_PICTURE);
Let me know, this helps...
I'm currently developing a J2ME app. I'm having problems with file uploading. I dont seem to know what part of my code is wrong. Here is my code:
public void UploadImage(long newFileId, String url, String bytes){
HttpConnection conn = null;
OutputStream os = null;
InputStream s = null;
StringBuffer responseString = new StringBuffer();
try
{
System.out.println(System.getProperty("HTTPClient.dontChunkRequests"));
conn.setRequestMethod(HttpConnection.POST);
conn = (HttpConnection)Connector.open(url);
conn.setRequestProperty("resumableFileId", ""+newFileId);
conn.setRequestProperty("resumableFirstByte", ""+0);
conn.setRequestProperty("FilePart", bytes);
// Read
s = conn.openInputStream();
int ch, i = 0, maxSize = 16384;
while(((ch = s.read())!= -1 ) & (i++ < maxSize))
{
responseString.append((char) ch);
}
conn.close();
System.out.println(responseString.toString());
String res = uploadFinishFile(newFileId, bytes);
if(res.length()>0)
System.out.println("File uploaded.");
else
System.out.println("Upload failed: "+res);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
This is the java code that im trying to convert to j2me:
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntity me = new MultipartEntity();
StringBody rfid = new StringBody("" + newFileId);
StringBody rfb = new StringBody("" + 0);
InputStreamBody isb = new InputStreamBody(new BufferedInputStream(new FileInputStream(f)), "FilePart");
me.addPart("resumableFileId", rfid);
me.addPart("resumableFirstByte", rfb);
me.addPart("FilePart", isb);
post.setEntity(me);
HttpResponse resp = client.execute(post);
HttpEntity resEnt = resp.getEntity();
String res = da.uploadFinishFile(login, password, newFileId, DigestUtils.md5Hex(new FileInputStream(f)));
if(res.isEmpty())
System.out.println("File uploaded.");
else
System.out.println("Upload failed: "+res);
} catch (Exception ex) {
System.out.println("Upload failed: "+ex.getMessage());
}
You are uploading the file passing the parameters as HTTP headers, instead of sending the image in the HTTP message body using multipart file upload, compatible with the code you're converting.
Take a look at HTTP Post multipart file upload in Java ME. You can use the HttpMultipartRequest class and change your code to:
Hashtable params = new Hashtable();
params.put("resumableFileId", "" + newFileId);
params.put("resumableFirstByte", "" + 0);
HttpMultipartRequest req = new HttpMultipartRequest(
url,
params,
"FilePart", "original_filename.png", "image/png", isb.getBytes()
);
byte[] response = req.send();
I have a web service that get a audio and do some operation on it and finally returns a string ,this web service have a web page like this
<form name="form1" method="post" action="Default.aspx" id="form1" enctype="multipart/form-data">
<input type="file" name="FileUpload3" id="FileUpload3" style="width:325px;" />
<input type="submit" name="Button6" value="Upload File" id="Button6" />
<span id="Label1"></span>
</form>
when file choose for uploadfile3 and press upload file a same page should be reload and then show the string in span lable, I want to connect this web service by android so I tried below code to connect and upload file, the server response 200 code but no file uploads to server and no string shows, it seems that server press upload file without choosing file, what can I do? help please.
public void upLoad2Server() throws ClientProtocolException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://11.12.13.174/file_transfer_sample/ClientWebSite/Default.aspx");
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/test.wav");
ContentBody cbFile = new FileBody(file, "audio/wav");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("FileUpload3", cbFile);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
Log.i("status", String.valueOf(response.getStatusLine()));
}
Check this code to upload file from Android to Web Server
public class UploadFileToServer extends AsyncTask<Object, String, Object>
{
URL connectURL;
String params;
String responseString;
String fileName;
byte[] dataToServer;
FileInputStream fileInputStream;
private int serverResponseCode;
private String serverResponseMessage;
private static final String TAG = "Uploader";
public void setUrlAndFile(String urlString, File fileName)
{
Log.d(TAG,"StartUploader");
try
{
fileInputStream = new FileInputStream(fileName);
connectURL = new URL(urlString);
}
catch(Exception e)
{
e.getStackTrace();
publishProgress(e.toString());
}
this.fileName = fileName.getAbsolutePath()+".txt";
}
synchronized void doUpload()
{
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
Log.d(TAG,"lv1");
try
{
Log.d(TAG,"doUpload");
publishProgress("Uploading...");
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.d(TAG,"LvA");
Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer,0, bufferSize);
Log.d(TAG,"LvB");
while(bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
int ch;
Log.d(TAG,"LvC");
StringBuffer buff = new StringBuffer();
while((ch=is.read()) != -1)
{
buff.append((char)ch);
}
// publishProgress(buff.toString());
dos.close();
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
serverResponseMessage = conn.getResponseMessage();
// Log.d(TAG,"Buffer "+buff.toString());
Log.d(TAG,"Server Response "+serverResponseMessage);
}
catch(Exception e)
{
e.getStackTrace();
publishProgress(e.toString());
}
}
#Override
protected Object doInBackground(Object... arg0)
{
Log.d(TAG,"lv1a");
doUpload();
Log.d(TAG,"Uploading Completed! Path: "+connectURL);
return null;
}
protected void onProgressUpdate(String... progress)
{
//this.info.setText(progress[0]);
Log.d("Progress", progress[0]);
}
}
Thanks
Check this code.It's upload a file to .NET httphandler server. It uses SSL self-signed security, but it's an example how to use methods, and multipart entity. It works for me.
public static void main(String[] args) throws IOException {
try {
File f = new File(
"c:\\eula.1028.txt");
System.out.println("LENGTH " + f.length());
PostMethod method = new PostMethod("/Handler");
FilePart filePart = new FilePart("file",f);
filePart.setContentType("application/pdf");
Part[] parts = {filePart};
MultipartRequestEntity request = new
MultipartRequestEntity(parts, method.getParams());
method.setRequestEntity(request);
Protocol easyhttps = new Protocol("https",
new EasySSLProtocolSocketFactory(), 2000);
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHostConfiguration().setHost("localhost", 2000, easyhttps);
client.executeMethod(method);
String s = method.getResponseBodyAsString();
System.out.println(s);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
In my app, I download the HTML Stylesheet of a website, using this code:
private DefaultHttpClient createHttpClient() {
HttpParams my_httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry);
DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams);
return httpclient;
}
private class Example extends AsyncTask<Void, Void, Void> {
int mStatusCode = 0;
String content = "";
#Override
protected Void doInBackground(Void... args) {
String url = "www.example.com";
DefaultHttpClient httpclient = createHttpClient();
HttpGet httpget = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
mStatusCode = statusLine.getStatusCode();
if (mStatusCode == 200){
content = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void arg) {
//Stuff
}
}
However, sometimes, especially when the phone is on 3g, I'm getting mStatusCode = 0, while other internet apps such a the browser still work.
Do you guys know how I could prevent this from happening?
Many many thanks in advance!!
For parsing html you can use jsoup - Java HTML Parser. For example:
String url = "http://www.google.com";
Document doc = Jsoup.connect(url).get();
Elements img = doc.select("img");
Elements js = doc.select("script");
// Save images
for (Element el : img)
{
String imageUrl = el.attr("src");
FileUtils.saveFile("url", "File", "Folder");
}
// Save JS
for (int j = 0; j < js.size() - 1; j++)
{
String jsUrl = js.get(j).attr("src");
FileUtils.saveFile("url", "File", "Folder");
}
// The same for CSS
For saving file:
public static void saveFile(String fileUrl, String fileName,
String folderName) throws IOException
{
URL url = new URL(fileUrl);
InputStream input = url.openStream();
File extStorageDirectory = Environment.getExternalStorageDirectory();
File folder = new File(extStorageDirectory, folderName);
folder.mkdir();
OutputStream output = new FileOutputStream(new File(folder, fileName));
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0)
{
output.write(buffer, 0, bytesRead);
}
output.close();
input.close();
}
For checking network availability you can use this method:
public static boolean checkIfURLExists(String host, int seconds)
{
HttpURLConnection httpUrlConn;
try
{
httpUrlConn = (HttpURLConnection) new URL(host).openConnection();
// Set timeouts in milliseconds
httpUrlConn.setConnectTimeout(seconds * 1000);
httpUrlConn.setReadTimeout(seconds * 1000);
// Print HTTP status code/message for your information.
System.out.println("Response Code: " + httpUrlConn.getResponseCode());
System.out.println("Response Message: "
+ httpUrlConn.getResponseMessage());
return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
return false;
}
}
I am trying to upload an image (multi-part/form-data) using httpClient library. I am able to upload the image using httpPost Method and a byteArrayRequestEntity. Following is the code I used:
File file = new File(imageFilePath);
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("https://domain/link/folderId/files?access_token="+accessToken);
method.addRequestHeader("Content-Type","multipart/form-data;boundary=AaB03x");
String boundary = "AaB03x";
StringBuilder builder = new StringBuilder();
builder.append("--");
builder.append(boundary+"\r\n");
builder.append("Content-Disposition: form-data; name=\"file\"; filename=\"photo.jpg\"");
builder.append("\r\n");
builder.append("Content-Type: image/jpeg");
builder.append("\r\n");
builder.append("\r\n");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(builder.toString().getBytes("utf-8"));
builder.setLength(0);
InputStream is = new FileInputStream(file);
byte[] buffer = new byte[4096];
int nbRead = is.read(buffer);
while(nbRead > 0) {
baos.write(buffer, 0, nbRead);
nbRead = is.read(buffer);
}
is.close();
builder.append("\r\n");
builder.append("--");
builder.append(boundary);
builder.append("--");
builder.append("\r\n");
baos.write(builder.toString().getBytes("utf-8"));
method.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), "multipart/form-data; boundary=\"" + boundary + "\""));
System.out.println(method.getRequestEntity().toString());
client.executeMethod(method);
But the project i am working on requires me to use an httpRequest and not Http PostMethod.
I tried with basicHttpEntityEnclosingRequest, but the setEntity method for the same accepts only a httpEntity (i was using ByteArrayRequestEntity).
Could anyone help me with how to modify the code so that it uses a HttpRequest (or its subtype) instead of a PostMethod?
- I have used apache-mime library for posting the image with message to the Webserver.
Here is the code from my production environment:
public String postDataCreation(final String url, final String xmlQuery,final String path){
final StringBuilder sa = new StringBuilder();
final File file1 = new File(path);
Thread t2 = new Thread(new Runnable(){
public void run() {
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
FileBody bin1 = new FileBody(file1);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("dish_photo", bin1);
reqEntity.addPart("xml", new StringBody(xmlQuery));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
InputStream i = entity.getContent();
Log.d("Vivek", i.toString());
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
Log.d("YumZing", s);
sa.append(s);
}
Log.d("Check Now",sa+"");
}
catch (Exception ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
}
});
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Getting from Post Data Method "+sa.toString());
return sa.toString();
}