You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by deepaksawdekar <de...@infosys.com> on 2004/03/10 06:07:13 UTC

pdf file open.

Hi,
I am trying to open a pdf file. I am using servlet and struts in my application. Everything is working fine except it ask me two times do you want to open or save the file, once for the file whoes name is session id and second time for the actual file. 
Am i doing some thing wrong.
Please help me 


Servelet code is as follows
    
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException {

        //Method name 
        final String METHOD_NAME = CLASS_NAME.concat(".doGet()");

        String sessionId = request.getParameter(GUIConstants.USER_SESSION_ID);
        String encodefileName =
            request.getParameter(GUIConstants.USER_FILE_NAME);
        String fileName = new String();
        try {
            fileName =
                URLDecoder.decode(encodefileName, GUIConstants.CHAR_ENCODING);
        } catch (Exception ex) {
            LOGGER.error(METHOD_NAME, ex);
        }
        HttpSession session = request.getSession();

        if (!session.getId().equals(sessionId)) {
            // Don't do anything - just return
            //response.sendRedirect(GUIConstants.LOGIN_URL);
            return;
        }

        LOGGER.debug("File Name " + fileName);
        int dot = fileName.lastIndexOf(CommonConstants.DOT) + 1;        
        if(fileName.substring(dot).equalsIgnoreCase(CommonConstants.PDF)) {
        	response.setContentType(GUIConstants.PDF_CONTENT_TYPE);
        } else {
        	response.setContentType(GUIConstants.CONTENT_TYPE);
        }        
        response.setHeader(
            GUIConstants.CONTENT_DIS,
            GUIConstants.CONTENT_ATT + "\"" + fileName + "\"");

        try {
            String filePath =
                GetProperties.getValue(
                    PropertiesFileConstants.PMP_PROP_FILE_PATH,
                    CommonConstants.DOCUMENT_LOCAL_STORE_PATH_FOR_VIEWING);
            ServletOutputStream out = response.getOutputStream();
            String fullFilePath = filePath + File.separator + fileName;
            FileInputStream fin = new FileInputStream(new File(fullFilePath));
            byte[] buf = new byte[GUIConstants.FILE_SIZE];
            int len = 0;
            while ((len = fin.read(buf)) != -1) {
                out.write(buf);
            }
            out.flush();
            fin.close();

        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error(e.getMessage(), e);
        }

    }

    /**
     * @param req HttpServletRequest
     * @param res HttpServletResponse
     * @throws ServletException servlet exception
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException {
        doGet(req, res);
    }


Thanks and Regards
Deepak.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: pdf file open.

Posted by HG <st...@nospam.websolver.dk>.
Hi

I don't know if this helps...But I have read somewhere, that you must set up
the content-length header of your response, when doing PDF streaming.

Just set it as the size of your FileStream (or another stream object)

Try it out..and let me know..





----- Original Message ----- 
From: "deepaksawdekar" <de...@infosys.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Wednesday, March 10, 2004 6:07 AM
Subject: pdf file open.


Hi,
I am trying to open a pdf file. I am using servlet and struts in my
application. Everything is working fine except it ask me two times do you
want to open or save the file, once for the file whoes name is session id
and second time for the actual file.
Am i doing some thing wrong.
Please help me


Servelet code is as follows

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException {

        //Method name
        final String METHOD_NAME = CLASS_NAME.concat(".doGet()");

        String sessionId =
request.getParameter(GUIConstants.USER_SESSION_ID);
        String encodefileName =
            request.getParameter(GUIConstants.USER_FILE_NAME);
        String fileName = new String();
        try {
            fileName =
                URLDecoder.decode(encodefileName,
GUIConstants.CHAR_ENCODING);
        } catch (Exception ex) {
            LOGGER.error(METHOD_NAME, ex);
        }
        HttpSession session = request.getSession();

        if (!session.getId().equals(sessionId)) {
            // Don't do anything - just return
            //response.sendRedirect(GUIConstants.LOGIN_URL);
            return;
        }

        LOGGER.debug("File Name " + fileName);
        int dot = fileName.lastIndexOf(CommonConstants.DOT) + 1;
        if(fileName.substring(dot).equalsIgnoreCase(CommonConstants.PDF)) {
        response.setContentType(GUIConstants.PDF_CONTENT_TYPE);
        } else {
        response.setContentType(GUIConstants.CONTENT_TYPE);
        }
        response.setHeader(
            GUIConstants.CONTENT_DIS,
            GUIConstants.CONTENT_ATT + "\"" + fileName + "\"");

        try {
            String filePath =
                GetProperties.getValue(
                    PropertiesFileConstants.PMP_PROP_FILE_PATH,
                    CommonConstants.DOCUMENT_LOCAL_STORE_PATH_FOR_VIEWING);
            ServletOutputStream out = response.getOutputStream();
            String fullFilePath = filePath + File.separator + fileName;
            FileInputStream fin = new FileInputStream(new
File(fullFilePath));
            byte[] buf = new byte[GUIConstants.FILE_SIZE];
            int len = 0;
            while ((len = fin.read(buf)) != -1) {
                out.write(buf);
            }
            out.flush();
            fin.close();

        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error(e.getMessage(), e);
        }

    }

    /**
     * @param req HttpServletRequest
     * @param res HttpServletResponse
     * @throws ServletException servlet exception
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException {
        doGet(req, res);
    }


Thanks and Regards
Deepak.

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org