Upload image on a web site using Java - java

Is there any possibility to upload a file (for example: an image), to a site and to calculate the transfer rate?
I have some code that downloads an image from a specified url and calculates the transfer rate, using the java.net.Url class, something like:
long startTime = System.currentTimeMillis(); //start time
System.out.println("Connecting site...\n");
System.out.println("Downloading......");
URL url = new URL("http://....");
url.openConnection();
InputStream reader = url.openStream();
FileOutputStream writer = new FileOutputStream("D:/imagine.jpg");
byte[] buffer = new byte[153600];
int totalBytesRead = 0;
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0)
{
writer.write(buffer, 0, bytesRead);
buffer = new byte[153600];
totalBytesRead += bytesRead;
}
long endTime = System.currentTimeMillis();//end of download
long elapsedTime=(endTime-startTime)/1000;//from miliseconds in seconds
System.out.println("ElapsedTime is " +elapsedTime +" s");
int memory=new Integer(totalBytesRead);
double memoryFinal=memory * 0.0009765625; //file in Kb
System.out.println("File size: " +memoryFinal +"Kb");
System.out.println("Speed :" + memoryFinal/elapsedTime + "Kbps");
writer.close();
reader.close();
I need something easy and useful. Thank you.

Yes you can - but it is not simple.
POSTing a file to a server is not implemented in plain java URLConnection, but you have to implements the protocol.
Or, You can use org.apache.commons.httpclient
http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload

I recommend the library Apache FileUpload.
You can implement a progress bar too. See this .
Regards

Related

Improving the performance of read()

I'm trying to write a proxy-application for Android.
I created a ServerSocket that listens on the localhost and a port.
When a browser requests a site, I open a new thread with the Socket and read the inputstream.
The problem: The read-call is too slow. It needs up to one second.
I don't think the browser's outputstream is that slow.
public Request readRequest() {
int length;
byte[] buffer = new byte[8192];
long startTime = System.nanoTime(); // Messure starts
while ((length = in.read(buffer)) != 0) {
Log.d("PERFORMANCE", "read() needs " + (System.nanoTime() - startTime)/1000000 + " ms for: " + length + " bytes"); // Messure ends
Request request = Request.parse(buffer, length);
if (request != null) {
// read bytes contained a complete Request
return request;
}
// request is incomplete -> read more
startTime = System.nanoTime();
}
return null;
}
I thought it might could be a sheduling problem, so i already tried to increase the priority of the current Thread. It slightly improved the speed.
Is there another way to decrease the idle time or latency?
What about NDK/JNI?
First thing I want to give a note:
int length;
should be IMO:
long length = 0L;
Solution for your problem could be increasing the buffer size for byte[] buffer = new byte[8192];
Also maybe Request request = Request.parse(buffer, length); could be slowing down everything.

Can a byte stream be written straight to SDCard from HTTP bypassing the HEAP?

I'm downloading video files that are larger than the memory space that Android apps are given. When they're *on the device, the MediaPlayer handles them quite nicely, so their overall size isn't the issue.
The problem is that if they exceed the relatively small number of megabytes that a byte[] can be then I get the dreaded OutOfMemory exception as I download them.
My intended solution is to just write the incoming byte stream straight to the SD card, however, I'm using the Apache Commons library and the way I'm doing it tries to get the entire video read in before it hands it back to me.
My code looks like this:
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod(URL_PATH);
client.setConnectionTimeout(timeout);
byte [] ret ;
try{
if(nvpArray != null)
filePost.setRequestBody(nvpArray);
}catch(Exception e){
Log.d(TAG, "download failed: " + e.toString());
}
try{
responseCode = client.executeMethod(filePost);
Log.d(TAG,"statusCode>>>" + responseCode);
ret = filePost.getResponseBody();
....
I'm curious what another approach would be to get the byte stream one byte at a time and just write it out to disk as it comes.
You should be able to use the GetResponseBodyAsStream method of your PostMethod object and stream it to a file. Here's an untested example....
InputStream inputStream = filePost.getResponseBodyAsStream();
FileInputStream outputStream = new FileInputStream(destination);
// Per your question the buffer is set to 1 byte, but you should be able to use
// a larger buffer.
byte[] buffer = new byte[1];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1)
{
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();

Pause download in Java?

I'm using this code to download a file, and I was wondering if its possible to pause the download and then resume it later, and if so how?:
URL url = new URL(URL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
lenghtOfFile /= 100;
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Path + FileName);
byte data[] = new byte[1024];
long total = 0;
int count = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
total += count;
}
output.flush();
output.close();
input.close();
This is only possible if the server supports HTTP range headers (introduced in HTTP 1.1)
See this question for examples in Java: how to use the HTTP range header in J2ME?.
"Pausing" could just mean reading some of the stream and writing it to disk. When resuming you would have to use the headers to specify what is left to download.
I think the support for the resume is at the server side, the clients cannot direct it.

Java File Upload using socket,Percentage of uploded file required?

Hii i am uploading a file to server using socket and i need the percent of file loaded?how can i do that?i have the maximun value i.e the file length ,how can i get how much file has been uploaded?
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
while ((i = in.read()) != -1) {
publishProgress(???);
out.write(i);
System.out.println(i);
}
I need to pass the length of file uploded in the publishProgress method.
using buffered copying
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
int written = 0;
byte[] buf = new byte[512];
while ((i = in.read(buff)) != -1) {
out.write(buff,0,i);
written += i;
publishProgress((double)written/length);
//passing a double value from 0-1 to say how much is transmitted (length is length of file)
System.out.println(buff+", "+i);
}
To do this you need to do one of a couple of things:
Use a Flash uploader such as swfupload (see http://demo.swfupload.org/Documentation/) as these typically provide access to upload progress of this sort.
Provide a back channel of your own: perform the form submit with Ajax and then while the form submit occurs you run a javascript timer that hits a URL on the server with a key of some kind that corresponds to the upload. The URL on the server looks up how much of the file has been uploaded and returns that number and you pass that through to your uploadProgress method.
Below is your modified code. written holds the number of ints written to the socket.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
int written = 0;
while ((i = in.read()) != -1) {
out.write(i);
publishProgress(++written);
System.out.println(i);
}
javax.swing.ProgressMonitorInputStream

Updating progress dialog

I am trying to make an application that can help me to evaluate the time to download the file from a web resource. I have found 2 samples:
Download a file with Android, and showing the progress in a ProgressDialog
and
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
The second example shows a smaller download time, but I cannot understand how to update progress dialog using it. I think something should be done with "while" expression in second case, but I cannot find what. Could someone give me any piece of advice?
UPD:
1st code:
try {
time1 = System.currentTimeMillis();
URL url = new URL(path);
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/analyzer/test.jpg");
byte data[] = new byte[1024];
long total = 0;
time11 = System.currentTimeMillis();
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
time22= System.currentTimeMillis()-time11;
output.flush();
output.close();
input.close();
} catch (Exception e) {}
timetaken = System.currentTimeMillis() - time1;
2nd code:
long time1 = System.currentTimeMillis();
DownloadFromUrl(path, "test.jpg");
long timetaken = System.currentTimeMillis() - time1;
Where
public void DownloadFromUrl(String imageURL, String fileName) { //this is the downloader method
try {
URL url = new URL(imageURL); //you can write here any link
File file = new File(fileName);
/*Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(PATH+file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
So the thing is that first method seems to be slower for about 30%.
The second example may run faster, but it monopolizes the GUI thread. The first approach, using AsyncTask, is better; it allows the GUI to stay responsive as the download proceeds.
I found it helpful to compare AsyncTask with SwingWorker, as shown in this example.
first link is best. But i can't provide code( it's home comp) in monday or later i can provide full function. But :
private class DownloadFile extends AsyncTask<String, Integer, String>{
#Override
protected String doInBackground(String... url) {
int count;
try {
URL url = new URL(url[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
this class are best for it ( imho) . publishProgress it's simple function where u have max two lines. Set max and set current. How u can see in this code lenghtOfFile it's how many bytes have ur file. total-current progress ( example 25 from 100 bytes) . Run this class easy : DownloadFile a = new DownloadFile(); a.execute(value,value);//or null if u not using value. Hope u understand me , im not good speaking on english.

Categories