Java

"HttpClient" 를 이용하여 어플리 케이션 실행화일 업데이트 방법

안.들 2015. 11. 23. 14:14

"HttpClient" 를 이용하여 어플리 케이션 실행화일 업데이트 방법

 

@호출Stack

AppMain(어플리케이션 메인), updateApp(업데이트모듈)

AppMain에서 업데이트 event 수행 => updateApp호출 => AppMain 종료 => updateApp수행 실행파일 다운로드(기존 실행파일 대체됨) => AppMain 실행

 

 

AppMain



        public void actionPerformed(ActionEvent e) {

                             

                              if(0 == uMessage.MsgPopup("업데이트 하시겠어요?", uMessage.MsgOption.OK_CANCEL)){

                                     

                                      File file = new File("oneDevCenterUpdate.exe");

                                      String sPath = file.getPath();

                                     

                                      Process p;

                                             try {

                                                    

                                                     //업에이트 수행 프로그램을 실행한다.

                                                     p = newProcessBuilder(sPath).start();

                                                    

                                                     //기존 메인 시스템종료한다.

                                                     System.exit(0);                                      

                                        

                                             } catch (IOException e1) {

                                                     // TODO Auto-generated catch block

                                                     e1.printStackTrace();

                                             }             

                                     

                              }

                       }

 

 

 

updateApp

 

http 통신/File IO 라이브러리 참조

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

 

 

        public static void main(String[] args) throwsIOException {

               // TODO Auto-generated method stub

              

               File file = new File("oneDevCenter.exe");

               String sPath = file.getAbsolutePath();

              

               HttpClient httpclient = newDefaultHttpClient();

               HttpGet httpget = new HttpGet("http://??????/oneDevCenter.exe");

               HttpResponse response = httpclient.execute(httpget);

               System.out.println(response.getStatusLine());

               HttpEntity entity = response.getEntity();

               if (entity != null) {

                    InputStream instream = entity.getContent();

                    try {

                        BufferedInputStream bis = newBufferedInputStream(instream);

                        String filePath =sPath;

                        BufferedOutputStream bos = newBufferedOutputStream(newFileOutputStream(new File(filePath)));

                        int inByte;

                        while ((inByte = bis.read()) != -1 ) {

                            bos.write(inByte);

                        }

                        bis.close();

                        bos.close();

                       

                    } catch (IOException ex) {

                        throw ex;

                    } catch (RuntimeException ex) {

                        httpget.abort();

                        throw ex;

                    } finally {

                        instream.close();

                    }

                    httpclient.getConnectionManager().shutdown();

                    exeCuteAfterJavaStudy();

               }

        }

 

 

AppMain수행 : 파일의 위치를 찾고 수행한다.

 

       

        public static void exeCuteAfterJavaStudy()

        {

               Process p;

                       try {

                             

                              File file = newFile("oneDevCenter.exe");

                              String sPath = file.getAbsolutePath();

                             

                              p = newProcessBuilder(sPath).start();

                                            

                              System.out.println("Process Done");

                 

                       } catch (IOException e1) {

                              // TODO Auto-generated catch block

                              e1.printStackTrace();

                       }             

              

        }

 

 

 

 

 

Httpclient 는 http 프로토콜을 지원해주는 클라이언트 객체임.

해당 라이브러리를 응용해서 다양하게 구현해 보시기 바랍니다. (예, screen scrapping)

 

 

HttpClient library 링크:

Java :

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html

microsoft : http://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx