Friday, January 31, 2014

Image Upload Code From Local pc

Jsp page
<s:form action="userImage" method="post" enctype="multipart/form-data">
<div id="Row_9_B" style="float:left;width:527px;height:auto;margin-bottom:2px;display:none;">
              <div id="" style="float:left;width:150px;height:auto;text-align:right; " class="fontSize1">Please specify a file:</div>
              <div id="" style="float:left;width:325px;height:auto;text-align:left;" class="fontSize1">
<!--input type="file" name="datafile" size="25"-->
<div style="float:left; width:250px; text-align:right;">
<s:file  id="userImage" name="userImage" label="Patient Image" />
</div>
<div style="float:left; width:75px;" >
<!--input  id="take" name="" value="Submit" type="button" onclick="takeLoadImage('userImage')"/  USE FOR SCRIPT -->

</div>
</div>
</div>       
     
     
     
      <div id="Row_PriorApp" style="float:left;width:527px;height:auto;margin-bottom:2px;">
              <div id="" style="float:left;width:480px;height:auto;text-align:right;" class="fontSize1"><s:actionerror /></div>
              <div id="hereImageSet_div" style="float:left;width:480px;height:auto;text-align:left; text-align:center;" class="fontSize1">
              </div>
      </div>
</s:form>
Info: struts default method is execute
struts file
 <!--  My Change -->
        <action name="userImage" class="com.allClass.Action.ImageLoadAction">
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">2097152</param>
                <param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success">/Patient/registration.jsp</result>
            <result name="input">/Patient/registration.jsp</result>
        </action>


ImageLoadAction.java
package com.allClass.Action;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.allClass.Model.ReasonAppPatientInfo;
import com.opensymphony.xwork2.ActionSupport;

import Database.DatabaseConnection;
import SQLQuery.Patient.ReasonAppointmentSql;

// extends ActionSupport (This part use fot addActionError also use tag in jsp page N.B:<s:actionerror />)

public class ImageLoadAction extends ActionSupport implements ServletRequestAware {
   
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    //private String userImageFileName;

    private HttpServletRequest servletRequest;
    private Connection con = null;
    private DatabaseConnection dbc = new DatabaseConnection();
   

   
    public String execute() {
        System.out.println("Name");
    try {

        String filePath = servletRequest.getRealPath("/");
        //String filePath = "c:/Rokon";
       
        System.out.println("Server path:" + filePath);
        File fileToCreate = new File(filePath, this.userImageFileName);

        if(userImage !=null){      // No need this line first time condition base
            FileUtils.copyFile(this.userImage, fileToCreate);
           
                 //insert image into database
           
                //String filePath = servletRequest.getRealPath("/");
                filePath += userImageFileName;           
                File image = new File(filePath);
                System.out.println(image);
                FileInputStream   fis = new FileInputStream(image);
               
                //insert image into database
                //======================
                con = dbc.connectDB();
                String sql = "insert into mypictures (photo) values (?)";
                PreparedStatement stmt = con.prepareStatement(sql);
                stmt.setBinaryStream(1, fis, (int) image.length());
               
                stmt.execute();

                con.commit();
                fis.close();
                con.close();
        } // No need this line first time condition base
        //String filePath1 = servletRequest.getRealPath("/");
        //String filePath1 = "c:/Rokon/";
       
    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());
       
        appSuccessPage();
        return INPUT;
    }
    appSuccessPage();
    return SUCCESS;
}

   
    //  Not database save
    /*public String execute() {
        try {

            String filePath = servletRequest.getRealPath("/");
            //String filePath = "c:/Rokon";
           
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, this.userImageFileName);

            FileUtils.copyFile(this.userImage, fileToCreate);
            //String filePath1 = servletRequest.getRealPath("/");
            //String filePath1 = "c:/Rokon/";
             con = dbc.connectDB();
               
                String sql = "insert into MYPICTURES (photo) values (?)";
                PreparedStatement stmt = con.prepareStatement(sql);
              
                //String filePath = servletRequest.getRealPath("/");
                filePath += userImageFileName;
                File image = new File(filePath);
                System.out.println(image);
                FileInputStream   fis = new FileInputStream(image);
                stmt.setBinaryStream(1, fis, (int) image.length());
                stmt.execute();

                con.commit();
                fis.close();
                con.close();
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());

            return INPUT;
        }
        return SUCCESS;
    }*/
   
   
   
   
   
   
   
    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;

    }

    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }


   
   
    // My Start **************
   
   
    public void appSuccessPage(){
        System.out.println("Appointment Success Page .. . .. ");
        ReasonAppointmentSql reasonAppointmentSql = new ReasonAppointmentSql();
        reasonAppointmentSql.addPatientA(reg_no,dob,gender,patType_id,docChember_id,sp_dr,reason_consul,ch_complain);
        //return "appSuccessPage";
       
    }
   
    private List patientSeqList = new ArrayList<ReasonAppPatientInfo>();
    public void serialMaintainPage(){
        System.out.println("Serial No Azax Page:  return List ");
        ReasonAppointmentSql reasonAppointmentSql = new ReasonAppointmentSql();
        patientSeqList=reasonAppointmentSql.serialMain(speciality_id);
        //return "serialMaintainPage";
    }
   
    private String reg_no;
    private String  dept_id;
    private String  speciality_id;
    private String  sp_dr;
    private String  superen_id;

    private String  medicalOfficer_id;
    private String  ch_complain;
    private String  reason_consul;


    private String  docChember_id;
    private String  gender;
    private String  dob;
    private String  patType_id;
   



    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        this.con = con;
    }

    public DatabaseConnection getDbc() {
        return dbc;
    }

    public void setDbc(DatabaseConnection dbc) {
        this.dbc = dbc;
    }

    public String getReg_no() {
        return reg_no;
    }

    public void setReg_no(String reg_no) {
        this.reg_no = reg_no;
    }

    public String getDept_id() {
        return dept_id;
    }


    public void setDept_id(String dept_id) {
        this.dept_id = dept_id;
    }

    public String getSpeciality_id() {
        return speciality_id;
    }

    public void setSpeciality_id(String speciality_id) {
        this.speciality_id = speciality_id;
    }

    public String getSp_dr() {
        return sp_dr;
    }

    public void setSp_dr(String sp_dr) {
        this.sp_dr = sp_dr;
    }

    public String getSuperen_id() {
        return superen_id;
    }

    public void setSuperen_id(String superen_id) {
        this.superen_id = superen_id;
    }


    public String getMedicalOfficer_id() {
        return medicalOfficer_id;
    }

    public void setMedicalOfficer_id(String medicalOfficer_id) {
        this.medicalOfficer_id = medicalOfficer_id;
    }

    public String getCh_complain() {
        return ch_complain;
    }

    public void setCh_complain(String ch_complain) {
        this.ch_complain = ch_complain;
    }

    public String getReason_consul() {
        return reason_consul;
    }

    public void setReason_consul(String reason_consul) {
        this.reason_consul = reason_consul;
    }

    public String getDocChember_id() {
        return docChember_id;
    }

    public void setDocChember_id(String docChember_id) {
        this.docChember_id = docChember_id;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getDob() {
        return dob;
    }


    public void setDob(String dob) {
        this.dob = dob;
    }


    public String getPatType_id() {
        return patType_id;
    }


    public void setPatType_id(String patType_id) {
        this.patType_id = patType_id;
    }

   
   
   
   
   

    public List getPatientSeqList() {
        return patientSeqList;
    }
    public void setPatientSeqList(List patientSeqList) {
        this.patientSeqList = patientSeqList;
    }


   
   
   
   
   
   
   
   
   
   
    //My  End **************   
   
   
}

web.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts2 Application</display-name>
    <listener>
        <listener-class>
            org.apache.struts2.tiles.StrutsTilesListener
        </listener-class>
    </listener>
    <context-param>
        <param-name>tilesDefinitions</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>

    <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>
    <welcome-file-list>
      <!--<welcome-file>UserImage.jsp</welcome-file>-->
        <welcome-file>pages/downloadPage.jsp</welcome-file>
        <!--<welcome-file>pages/Copy of downloadPage.jsp</welcome-file>-->
    </welcome-file-list>

</web-app>

If return Page is SuccessUserImage.jsp then -
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Success: Upload User Image</title>
</head>
<body>
    <%-- <h2>Struts2 File Upload Example</h2>
    User Image: <s:property value="userImage"/>
    <br/>
    Content Type: <s:property value="userImageContentType"/>
    <br/>
    File Name: <s:property value="userImageFileName"/>
    <br/> --%>
    Uploaded Image:
    <br/>
    <img src="<s:property value="userImageFileName"/>"/>

</body>
</html>
**/ Find the image in the following path server
Required :Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Project name

No comments:

Post a Comment