Advertisement
ipsBruno

(Java) Hospedagem de site em celulares

Sep 14th, 2012
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. /*
  2.  *  Copyright (c) 2012 [iPs]TeaM
  3.  *  Bruno da Silva (email@brunodasilva.com)
  4.  *  Sistema de hospedagem de arquivos http em CELULAR !!
  5.  *
  6.  * www.brunodasilva.com
  7.  * www.ips-team.forumeiros.com
  8. */
  9.  
  10.  
  11. import java.io.*;
  12.  
  13. import javax.microedition.io.*;
  14. import javax.microedition.midlet.*;
  15. import javax.microedition.lcdui.*;
  16. import javax.microedition.pki.*;
  17. import java.lang.IllegalArgumentException;
  18.  
  19. public class VisualMIDlet extends MIDlet implements CommandListener, Runnable {
  20.    
  21.     private Display                 displayCelular      ;
  22.     private Form                    formularioDisplay   ;
  23.  
  24.     private ServerSocketConnection  servidorSocket      ;
  25.  
  26.     private boolean             rodandoServidor = true  ;
  27.         private boolean             processandoPagina       ;
  28.         private String              porta = "80"            ;
  29.        
  30.     public void startApp() {
  31.         displayCelular = Display.getDisplay(this);
  32.  
  33.         if (formularioDisplay == null) {
  34.             formularioDisplay = new Form("VisualMIDlet");
  35.  
  36.             formularioDisplay.addCommand(new Command("Sair", Command.EXIT, 0));
  37.             formularioDisplay.setCommandListener(this);
  38.         }
  39.  
  40.         Thread t = new Thread(this);
  41.         t.start();
  42.  
  43.         displayCelular.setCurrent(formularioDisplay);
  44.     }
  45.  
  46.     public void pauseApp() {}
  47.  
  48.     public void destroyApp(boolean unconditional)
  49.         {
  50.         //    shutdown();
  51.         }
  52.  
  53.  
  54.     public void commandAction(Command c, Displayable s) {
  55.         if (c.getCommandType() == Command.EXIT) {
  56.                    
  57.                         rodandoServidor = false;
  58.                        
  59.                         try {
  60.                             servidorSocket.close();
  61.                         }
  62.                         catch (IOException ioe)  {
  63.                         }
  64.                        
  65.             notifyDestroyed();
  66.         }
  67.     }
  68.  
  69.     public void run() {
  70.            
  71.         try {
  72.                         formularioDisplay.append(new StringItem(null, "[x] Host Móvel ligado !! "));
  73.             servidorSocket = (ServerSocketConnection)Connector.open("socket://:" + porta);
  74.         }
  75.         catch ( Exception e ) {}          
  76.  
  77.         while ( rodandoServidor ) {
  78.  
  79.                         if( processandoPagina == false ) {
  80.                            
  81.                             processandoPagina = true;
  82.                            
  83.                             SocketConnection sc = null;
  84.                             InputStream entrada = null;
  85.                             Reader           in = null;
  86.                             HttpConnection http = null;
  87.                             PrintStream     out = null;
  88.  
  89.  
  90.                             try {
  91.                                     sc = (SocketConnection)servidorSocket.acceptAndOpen();
  92.  
  93.                                     in = new InputStreamReader(sc.openInputStream());
  94.                                     out = new PrintStream(sc.openOutputStream());
  95.  
  96.                                     out.print( "HTTP/1.1 200 OK\r\n\r\n" );
  97.                                    
  98.                                     StringBuffer tmpStr = new StringBuffer();
  99.  
  100.                                     int ch;
  101.                                    
  102.                                     while (-1 != (ch = in.read()) ) {                                          
  103.                                            
  104.                                             if ( (char) ch == '\n') break;
  105.                                            
  106.                                             else if( ( (char) ch != '\r') ) {
  107.                                                 tmpStr.append( (char) ch);
  108.                                             }
  109.                                     }
  110.                                    
  111.                                     if (tmpStr.length() != 0)
  112.                                     {
  113.  
  114.                                         String url = tmpStr.toString();
  115.                                         url  =  url.substring(5,url.length()-9) ;
  116.  
  117.                          
  118.                                         if(url.indexOf("favicon.ico") != -1) continue ;
  119.  
  120.                                                      if(url.length() < 2) {
  121.                                             url = "index.html";
  122.                                         }                                        
  123.                                         formularioDisplay.append(new StringItem(null, "[x] Conexão de entrada -" + sc.getAddress()));
  124.                                         formularioDisplay.append(new StringItem(null, "[x] Dados requisitados -" + url));
  125.  
  126.                                         out.print(file (url));
  127.                                         out.close();
  128.                                         sc.close();
  129.                                     }
  130.  
  131.                             }
  132.                            
  133.                             catch (Exception bla) {
  134.                                     out.close();
  135.                             }
  136.                         }
  137.                        
  138.                         processandoPagina = false;
  139.                        
  140.         }
  141.     }
  142.        
  143.        
  144.         private String file(String strs){
  145.            
  146.             InputStream is = getClass().getResourceAsStream("website/"+strs);
  147.            
  148.             StringBuffer sb = new StringBuffer();
  149.            
  150.             try{
  151.                 int chars, i = 0;
  152.                 while ((chars = is.read()) != -1){
  153.                     sb.append((char) chars);
  154.                 }
  155.                 return sb.toString();
  156.             }
  157.            
  158.             catch (Exception e) {
  159.             }
  160.            
  161.             return null;
  162.         }        
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement