Friday, 19 June 2015

How To Upload File Using Struts

===============================index.jsp=================================

<%-- 
    Document   : index
    Created on : Jun 17, 2015, 8:32:59 PM
    Author     : Anand
--%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Upload</title>
</head>
<body>
      <center>
   <form action="upload" method="post" enctype="multipart/form-data">
      <label for="myFile">Upload your file</label>
      <input type="file" name="myFile" />
      <input type="submit" value="Upload" onclick="run()"/>
   </form>
      </center>
</body>
</html>

<script>
    function run(){
        
        alert("Data Have Been Updated Sucessfully ")
    }
    
</script>


============================Web.xml=============================

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

===============================Struts.xml==============================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <package name="Anand" extends="struts-default">
   <action name="upload" class="Com.Anand.uploadFile">
       <result name="success">/index.jsp</result>
   </action>
   </package>
</struts>

==============================uploadFile.java========================

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Anand
 */
package Com.Anand;
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException; 

import com.opensymphony.xwork2.ActionSupport;

public class uploadFile extends ActionSupport{
    //Declare Prviate Varriable
   private File myFile;
   private String myFileContentType;
   private String myFileFileName;
   private String destPath;

   public String execute() throws IOException
   {
     // uploaded File Data Save on this Path 
      destPath = "F:\\upload";    //create folder on F drive with upload Name or else you can provide any folder name here 

      System.out.println("Src File name: " + myFile);
      System.out.println("Dst File name: " + myFileFileName);
      File destFile  = new File(destPath, myFileFileName);
      FileUtils.copyFile(myFile, destFile);
      
      return SUCCESS;
   }
   
   //Setter and getter method for All varriable 
   public File getMyFile() {
      return myFile;
   }
   public void setMyFile(File myFile) {
      this.myFile = myFile;
   }
   public String getMyFileContentType() {
      return myFileContentType;
   }
   public void setMyFileContentType(String myFileContentType) {
      this.myFileContentType = myFileContentType;
   }
   public String getMyFileFileName() {
      return myFileFileName;
   }
   public void setMyFileFileName(String myFileFileName) {
      this.myFileFileName = myFileFileName;
   }
}


please Find Attached Image for Folder Directroy in NetBeans 

if any Help Needed Mail on :ananddwivedi92@gmail.com







Wednesday, 13 May 2015

How to Acess All Files From Folder URL in Java


===========ReadAllFile.java===============

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author anand
 */
public class ReadAllFile {
   
    public static void main (String [] args) throws IOException{
       
        File folder = new File("/home/anand/Anand1");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  File file = listOfFiles[i];
  if (file.isFile() && file.getName().endsWith(".txt")) {
    String content = FileUtils.readFileToString(file);
   
      System.out.println("Read==>"+ content );
    /* do somthing with content */
  }
}
    }
}



==============================================
Make Sure That you create Anand1.txt and Anand2.txt inside Anand1 Folder then it will read the file from java code



How to Get Folder Name and Files Name From URL Using Java Code

===========GetFolder.java====================

package OrderDetails;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.json.simple.JSONObject;

/**
 *
 * @author ananddw
 */
public class GetFolder {

    // File directory = new File("/home/ananddw/NetBeansProjects/Welcome/web/SA-DeskConfiguration");
    public static void main(String[] args) {
        File currentDir = new File("/home/ananddwivedi/NetBeansProjects/Welcome/web/SA-DeskConfiguration"); // current directory
        displayDirectoryContents(currentDir);
    }

    public static void displayDirectoryContents(File dir) {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("Directory Name==>:" + file.getCanonicalPath());
                    displayDirectoryContents(file);
                } else {
                    System.out.println("file Not Access===>" + file.getCanonicalPath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Friday, 8 May 2015

How to Read Text/Properties File From JavaScript

==== fetch.html==========
<html>
<body>
  <div>"=="<span id="anand"/>"</div>
  <script src="hi.js"></script>
</body>
</html>

======hi.js ============
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


function requestListener() {
  document.querySelector("#anand").innerHTML = this.responseText;
};

onload = function() {
  var request = new XMLHttpRequest();
  request.onload = requestListener;
  request.open("GET", "content.properties", true);
  request.send();
};

=========anand.properties========
Name=Anand
Age= 26


Monday, 4 May 2015

How Working Directory and File seperator Works in Java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Anand;

import java.io.File;

/**
 *
 * @author Ananddw
 */
public class FileSep {
    public static void main (String [] args){
        // System.get property will give you Working Directory
        String wrkDir=System.getProperty("user.dir");
        //Print the working Directory
        System.out.println("WrkDir===>"+wrkDir);
        //Seperate file with "/" character.
        System.out.println("File Seperator===>"+File.separator);
        //seperate path with ":" Symbol
        System.out.println("File pathSeparator===>"+File.pathSeparator);
        //seperate file with "/" symbol
        System.out.println("File pathSeparatorChar===>"+File.pathSeparatorChar);
        //seperate file with ":" symbol
        System.out.println("File separatorChar===>"+File.separatorChar);
       
       
    }
   
}

Friday, 1 May 2015

how to bind you tube URL on HTML Page

==========================index.html================================

<!----  
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
@author: ananddw
-->

<!DOCTYPE html> 
<html> 
<body> 
<h3> Online video play from you tube using html</h3>

<iframe width="560" height="315" src="https://www.youtube.com/embed/0QgmDXzv2rg" frameborder="0" allowfullscreen></iframe>


</body> 
</html>
<style>
body{

        background-color: chocolate;
        alignment-adjust: central;
        width: auto;
}

iframe{
alignment-adjust: central;
}
</style>



How to insert Video in HTML

> First you create one folder inside that you put any video from your local system but try it support mp4 format .
> Create index.html file 
=====================index.html=========================
<!----  
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
@author: ananddw
-->

<!DOCTYPE html> 
<html> 
<body> 

<video width="1000" controls>
  <source src="Ariana Grande, The Weeknd - Love Me Harder.mp4" type="video/mp4">
  <source src="Ariana Grande, The Weeknd - Love Me Harder.mp4" type="video/ogg">
  
</video>
<p>
<a href="http://learnjavabyanand.blogspot.in/" target="_blank">Click here on Enter on Java World</a>.
</p>


</body> 

</html>
===============================================================
Run This file on your browser you will able to play any video through html code 


Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static M...