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...?

