Monday, August 30, 2010

File download using Struts

In some developments, you have to prompt "OPEN/SAVE" dialog for file viewing.
Here is a sample code for file download with the support of the "DownloadAction".

import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;


public class ExampleByteArrayDownload extends DownloadAction {

    protected StreamInfo getStreamInfo(ActionMapping mapping, 
                                       ActionForm form,
                                       HttpServletRequest request, 
                                       HttpServletResponse response)
            throws Exception {
   
        String fileName = httpServletRequest.getParameter("fileName");

        String fileExtension =
            fileName.substring(fileName.lastIndexOf("."), fileName.length());

        httpServletResponse.setHeader("Content-Disposition",
                                      "attachment;filename=\"" + fileName +
                                      "\"");
        
        // Download a "pdf" file
        String contentType = "application/pdf";
        byte[] myPdfBytes  = null;              // Get the bytes from somewhere

        return new ByteArrayStreamInfo(contentType, myPdfBytes);
        
    }

    protected class ByteArrayStreamInfo implements StreamInfo {
        
        protected String contentType;
        protected byte[] bytes;
        
        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }
        
        public String getContentType() {
            return contentType;
        }

        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }
}

Reference
http://wiki.apache.org/struts/StrutsFileDownload

Wednesday, July 28, 2010

Replace String pattern in "Vi"

1) Open the TEXT file using "vi" editor in linux.
2) Press ESC
3) Following command will replace "tab" character by "|".

    :%s/    /|/g

4) Save & quit file using following command.

    :wq!

Monday, July 19, 2010

Java property file usage

Use the below code to read the java property file and retrieve the property value.
 
      Properties properties = new Properties();
        FileInputStream in;
        try {
            in = new FileInputStream("app.properties");
            properties.load(in);
            in.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return properties.getProperty("appcode");

Sample "app.properties" file is shown below.

appcode=50
email=admin@yahoo.com

Tuesday, July 6, 2010

From date < To date validate using js

Use the below js code to validate date "From date < To date"

         var frmDate = document.getElementById("frmDate").value;
         var toDate = document.getElementById("toDate").value;

         if (frmDate < toDate) {
               alert("Invalid Date Range!\nStart Date cannot be after End Date!")
               return false;
           }

Wednesday, June 23, 2010

Solution to dwr session error

Edit your web.xml file and and the following code.

<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>

Sunday, May 9, 2010

Move large number of files in solaris using "awk"

  • Change directory where your files are located.
  • Use the following "awk" script to move files from one location to another.
  for k in `ls | awk '{print $1}'`; 
  do 
  mv $k destination;
  done

Monday, May 3, 2010

Use java 64 bit version

You have to use 64bit version of java when your memory allocation is larger than 4Gb.

Use -d64 parameter when running the application.

java -d64 -jar -Xms1024m -Xmx4096m Test.jar