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