file transfer through http

I have a java desktop application.

It has to get file throughhttp

It getsjar file.

Please, see my SSCCE:

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

*

* @author SShpk

*/

publicclass UpdateThroughHTTP{

privatestaticint CURRENT_VERSION = 1;

privatestaticint NEW_VERSION = 0;

privatestatic String urlString ="http://localrfcmd.ru/updateutility.html";

privatestatic String updateString ="http://localrfcmd.ru/MiNet.jar";

/** Creates a new instance of UpdateThroughHTTP */

public UpdateThroughHTTP(){

}

/**sets URL to update server

@param t format: http://site.org/updatepage.html

*/

publicstaticvoid setURL(String t){

urlString = t;

}

/**returns url to update server*/

publicstatic String getURL(){

return urlString;

}

/**sets URL to update server file*/

publicstaticvoid setUpdateString(String s){

updateString = s;

}

/**returns URL to update server file*/

publicstatic String getUpdateString(){

return updateString;

}

/**gets BufferedReader from server file*/

privatestatic BufferedReader getBufReaderFromServerFile(){

URL url =null;

try{

url =new URL(getUpdateString());

}catch (MalformedURLException ex){

ex.printStackTrace();

}

InputStream in =null;

try{

in = url.openStream();

}catch (IOException ex){

ex.printStackTrace();

}

InputStreamReader bufIn =new InputStreamReader(in);

BufferedReader br =new BufferedReader(bufIn);

return br;

}

/**checks web-page for version update info*/

publicstaticvoid getApplicationVersionThroughURL(){

int gotVersion=0;

URL url =null;

try{

url =new URL(getURL());

}catch (MalformedURLException ex){

ex.printStackTrace();

}

InputStream in =null;

try{

in = url.openStream();

}catch (IOException ex){

ex.printStackTrace();

}

InputStreamReader bufIn =new InputStreamReader(in);

BufferedReader br =new BufferedReader(bufIn);

String str;

try{

while( (str =br.readLine())!=null ){

System.out.println("line:::" + str +"\n");

if (str.contains("current version")){

Pattern p = Pattern.compile(" =\\d");

Matcher m = p.matcher(str);

boolean b =false;

while(b = m.find()){

gotVersion = Integer.valueOf(m.group().trim());

}

}

}

}catch (IOException ex){

ex.printStackTrace();

}

System.out.println("new Version is... " + gotVersion);

}

/**Copies update file to OS TEMP dir*/

publicstaticvoid getFileFromServer()throws IOException{

URL url =new URL(getUpdateString());//URL to file on the server

String tempDir = System.getProperty("java.io.tmpdir");//path to temp dir

System.out.println("TEMP = " + tempDir);

String fileName ="MiNet.jar";//filename of update file

String pathToTempFile = tempDir+fileName;//full path to file

File file =new File(pathToTempFile);//create new file

BufferedReader br=null;//BufferedReader for reading server file

String tmp =new String();//temp string for reading server file

if (file.createNewFile()){//if file creation succeed

}

FileOutputStream fOutStream =new FileOutputStream(pathToTempFile);//create OutputStream

PrintStream prStream =new PrintStream(fOutStream);//create writeStream

br = getBufReaderFromServerFile();//got BufReader from server file

while( (tmp=br.readLine())!=null ){//if file is not finished

prStream.println(tmp);//write string

System.out.println("strings: " + tmp);

}

fOutStream.close();//close OutputStream

}

publicstaticvoid main(String[] args){

//UpdateThroughHTTP.getApplicationVersionThroughURL();

String temp = System.getProperty("java.io.tmpdir");

try{

UpdateThroughHTTP.getFileFromServer();

}catch (IOException ex){

ex.printStackTrace();

}

}

}

It works fine, but file is corrupted. I understand, that I have to use binary streams, because jar file can have unreadable symbols.

I found some solutions withftp, but I must usehttp

Please, tell me, what classes I have to use to get binary Streams...?

[10261 byte] By [Holoda] at [2007-11-27 8:57:24]
# 1

You are correct, the Readers and Writers are corrupting the binary data. Use anything that extends InputStream to read, probably a BufferedInputStream around the HttpURLConnection's input stream, and a FileOutputStream to write. And don't try to read lines from a binary file, read into a byte[] array.

ejpa at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...
# 2

public static void getFile() throws IOException{

BufferedInputStream bufIn;

InputStream is;

HttpURLConnection con;

URL url = new URL("http://localrfcmd.ru/MiNet.jar");

con = (HttpURLConnection)url.openConnection();

con.connect();

is = con.getInputStream();

bufIn = new BufferedInputStream(is);

String tempDir = System.getProperty("java.io.tmpdir"); //path to temp dir

System.out.println("TEMP = " + tempDir);

String fileName = "MiNet.jar"; //filename of update file

String pathToTempFile = tempDir+fileName;//full path to file

FileOutputStream fileOut = new FileOutputStream(new File (pathToTempFile));

for (;;){

int data = bufIn.read();

if (data == -1){

break;

}else{

System.out.print ( (char) data);

fileOut.write(data);

}

}

}

This one method works fine!

my downloaded jar launches without any problems!

Thank you!

Holoda at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...
# 3
It'll work several times faster if you read into a byte array like I said, instead of writing a byte at a time to the file system.
ejpa at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...
# 4
Yes, I undersand I've read some forum posts about byte array soluton,but I did not understand how to read into byte array?how can I determine byte array size?
Holoda at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...
# 5

You don't have to determine it. Just use 4096 or 8192 like everybody else, and do reads and writes until you get EOF.

int count;

byte[] buffer = new byte[8192];

while ((count = in.read(buffer)) > 0)

out.write(buffer,0,count);

out.close();

in.close();

ejpa at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...
# 6
Ok!Thanks!I Will try!
Holoda at 2007-7-12 21:21:55 > top of Java-index,Core,Core APIs...