Tuesday 1 October 2013

File Upload

How to Upload Files using Java Servlets :


Hey Guys !!! Welcome again to my blog. Today I came to you with the Java Code of uploading files using Java Servlets. This post is very useful for all of you who wants to make a web application using Java Servlets & JSP. Almost every web application needs to upload the files which can be anything. So, if you want to prepare a web application using Java Servlets, you must know this concept of file uploading.

Some jar files need to be downloaded before we get start. The required jar files are given below :-
  1. commons-fileupload-1.2.2.jar
  2. commons-io-2.4.jar

Now set the classpath of the jar files you have downloaded from the link given above.

After setting the classpath, the only work we have to do is to code. So here we go.......

FileUpload.html :
 <html>  
 <body>  
   <h1>File Upload Form</h1>   
   <fieldset>  
     <legend>Upload File</legend>  
     <form action="FileUploadExample" method="post" enctype="multipart/form-data">  
       <label for="fileName">Select File: </label>  
       <input id="fileName" type="file" name="fileName" size="30" multiple="multiple" /><br/>        
       <input type="submit" value="Upload"/>  
     </form>  
   </fieldset>  
 </body>  
 </html>  


FileUploadExample.java :
 import java.io.File;  
 import java.io.IOException;  
 import java.io.PrintWriter;  
 import java.util.Iterator;  
 import java.util.List;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import org.apache.commons.fileupload.FileItem;  
 import org.apache.commons.fileupload.FileItemFactory;  
 import org.apache.commons.fileupload.FileUploadException;  
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
 import org.apache.commons.fileupload.servlet.ServletFileUpload;  
 public class FileUploadExample extends HttpServlet {  
      @Override  
     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
          throws ServletException, IOException {  
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
         response.setContentType("text/html");  
          PrintWriter out = response.getWriter();  
        if (isMultipart) {  
           // Create a factory for disk-based file items  
           FileItemFactory factory = new DiskFileItemFactory();  
           // Create a new file upload handler  
           ServletFileUpload upload = new ServletFileUpload(factory);  
          try {  
             // Parse the request  
             List /* FileItem */ items = upload.parseRequest(request);  
            Iterator iterator = items.iterator();  
            while (iterator.hasNext()) {  
              FileItem item = (FileItem) iterator.next();  
              if (!item.isFormField())  
               {  
                String fileName = item.getName();      
                String root = getServletContext().getRealPath("/");  
                File path = new File(root + "/uploads");  
                if (!path.exists())  
                 {  
                  boolean status = path.mkdirs();  
                }  
                File uploadedFile = new File(path + "/" + fileName);  
                System.out.println(uploadedFile.getAbsolutePath());  
                 if(fileName!="")  
                item.write(uploadedFile);  
                 else  
                   out.println("file not found");  
                 out.println("<h1>File Uploaded Successfully....:-)</h1>");  
              }  
               else  
               {  
                 String abc = item.getString();  
 //        out.println("<br><br><h1>"+abc+"</h1><br><br>");  
               }  
            }  
          } catch (FileUploadException e) {  
                out.println(e);  
          } catch (Exception e) {  
                out.println(e);  
          }  
        }  
         else  
         {  
           out.println("Not Multipart");  
         }  
      }  
 }  


Now you can compile & run the above codes using Apache Tomcat Server or any other server. the uploaded files will be in a folder named "uploads" inside the web-apps directory in Apache Tomcat folder.



I hope you will find this post helpful in various JAVA Projects. If you face any difficulty in this post you can ask it by commenting here. i will reply as soon as possible.

For more java codes................ stay connected  :-)

Monday 30 September 2013

SMS through JAVA

How to send SMS using JAVA :


Hello Guys !!! This tutorial shows you that how to send an SMS using Java. All that you need is a mobile phone and Dot Net Framework installed in your PC. You have to download some files for sending SMS using Java Code. The files are listed below :-
  1. Dot Net Framework
  2. jacob.dll
  3. Jacob.jar
  4. Regasm.exe
  5. Tom.dll
You have to download all of these to send sms using java. After finish downloading you have to follow some easy instructions given below to register the libraries for sending sms.

Follow these Instructions :

  1. Put Jocob.dll file in Java/jdk/bin and Java/jre/bin


  2. Set the classpath of the Jacob.jar file.


  3. Open Command Prompt in Administrator mode :- Click on start button then type cmd then do right-click on Command prompt and then click Run as Administrator.

    Note: If you do not open it administrator mode then you are not able to register those libraries.
  4. Register Tom.dll by following commands :-
             1.) cd \*path of tom.dll\* (ENTER)
                  Ex: cd c:\SMS

     2.) regasm tom.dll (ENTER)

     3.) regasm tom.dll /codebase (ENTER)










  • Check the com port of the connected phone modem :- The screen shots are given below to check the com port no.
    Open Control Panel


    Click on Phone & Modem option.

    Choose appropriate country and enter your std code in the first text box then click ok.

    Now open the tab Modems. Here you can find the com port no. of your phone modem (if connected). Here in this case, i will use my com port which is COM13.










  • Put the com port no. in the java program code whereever it is needed.










  • Run the program.


  • JAVA Code for Sending SMS :-

     import com.jacob.activeX.*;  
     import com.jacob.com.*;  
     class SendSMS  
     {  
        public static void main(String arg[])  
        {  
           ActiveXComponent comp = new ActiveXComponent("Tom.General");  
           System.out.println("The Library been loaded, and an activeX component been created");  
           String s="";  
           for(int i=0;i<3;i++)  
           {  
              s=Dispatch.call(comp,"SendSms","COM13,8889762181,SMS sending using java...").toString();  
              if(s.equals("true"))  
                System.out.println("Message send........");  
              else  
                System.out.println("Fail to Send Message..........");  
           }  
        }  
     }  
    


    I hope you will find this post helpful in various JAVA Projects. If you face any difficulty in this post you can ask it by commenting here. i will reply as soon as possible.

    For more java codes................ stay connected  :-)