Thursday, 30 April 2015

Program to rotate a 2-D square matrix by 90 degree

say you have a 4x4 two dimensional array, 
write a function that rotates it 90 degrees 
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]
Becomes:
[3][9][5][1]
[4][0][6][2]
[5][1][7][3]
[6][2][8][4] 
 
This is working code in C, which accepts the size 
of square matrix and matrix elements and prints
the rotated matrix.
#include<stdio.h>
#include<stdlib.h>

int main()
{
 int s, **arr, **arr2;
 int i, j, c, r;
 printf("\n Enter the size of square matrix : ");
 scanf("%d",&s);
 arr=(int *)malloc(s*sizeof(int));
 arr2=(int *)malloc(s*sizeof(int));
 printf("\n Enter the array elements :");
 for(i=0;i<s;i++)
 {
  arr[i]=(int *)malloc(s*sizeof(int));
  arr2[i]=(int *)malloc(s*sizeof(int));
 }
 for(i=0;i<s;i++)
  {
  printf("\n Enter the %d the row : ", i);
  for(j=0;j<s;j++)
   {
    c=s-1-i;
    r=j;
    scanf("%d",&arr[i][j]);
    arr2[r][c]=arr[i][j];
   }
 }

 for(i=0;i<s;i++)
 {
  printf("\n");
  for(j=0;j<s;j++)
  {
   printf("%d ", arr2[i][j]);
  }
 }
} 
 

Monday, 27 April 2015

JAVA : Handling Variable Arguments

Varargs was added in Java 5 and the syntax includes three dots  (also called ellipses).
Following is a program showing how to handle Variable arguments in Java :
public class testingVarargs {
public static void main(String args[]) {
   testFunction(1234, "India", "Delhi");
   testFunction(2345, "United States", "Mumbai", "kolkata");
    } 
public static void testFunction(int some, String... args) {
   System.out.print("\n" + some);
   for(String arg: args) {
    System.out.print(", " + arg);
  }
 }
}

Tuesday, 21 April 2015

Program to Upload files to Server using JSP & Servlets

This is a program to accept the file from user using JSP and then  uploads the file into the target server. It uses the jsch api.

You need to download the JSch api and include in the jars.

Upload.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<H1> APPLICATION TO PUSH FILES [ SAMS ]</H1>
        <form method="post" action="singleFileUpload" enctype="multipart/form-data">
          <br>  
            Select file to upload:
            <input type="file" name="uploadFile" />
            <br/><br/>
            <input type="submit" value="Upload" />
        </form>
    </center>
</body>
</html>

singleFileUpload.java

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;


/**
 * Servlet implementation class singleFileUpload
 */
@WebServlet("/singleFileUpload")
public class singleFileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
 
    public singleFileUpload() {
        super();
       }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = "user";
   String password = "xyz123";
   String host = "xx.yy.zz.qq";
   int port=22;
   String targetDir = "/user/doc";
   String lfile = null;
   String fileName = null;

        if(ServletFileUpload.isMultipartContent(request))
        {
            try {
 List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
           
               for(FileItem item : multiparts)
               {
                   if(!item.isFormField())
                   {
lfile = new File(item.getName()).getName();
                      
                        System.out.println("file to upload : " + lfile);
                      
                        String fieldName = item.getFieldName();
                        fileName = FilenameUtils.getName(item.getName());
                        InputStream fileContent = item.getInputStream();
                        System.out.println("File Content : " + fileContent.toString());
                        System.out.println("files to be uploaded : " + fileName);
                    
                        String fileNameDest=targetDir+"/"+fileName;
                        System.out.println(fileNameDest);
                        try
                     {
                         JSch jsch = new JSch();
                         Session session = jsch.getSession(user, host, port);
                         session.setPassword(password);
                         session.setConfig("StrictHostKeyChecking", "no");
                         System.out.println("Establishing Connection...");
                         session.connect();
                         System.out.println("Connection established.");
                         System.out.println("Crating SFTP Channel.");
                         ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
                         sftpChannel.connect();
                         System.out.println("SFTP Channel created.");
                       
                         sftpChannel.put(fileContent, fileNameDest);
                        
                       
                         sftpChannel.disconnect();
                         System.out.println("Disconnecting the sftp Chnannel");
                       
                         session.disconnect();
                         System.out.println("Discontinuing the session");
                   
                   }
                                     
                   catch( Exception e)
                   {
                   System.out.println(e);
                   }
               
                    }
                     
               
                }
             
            }
            catch (FileUploadException e)
            {
e.printStackTrace();
}        
        }
   }

}

All the best !!!