7 Replies Latest reply on Feb 15, 2009 12:58 AM by peterj

    upload file

    raffaele.letizia

      Hello everybody,
      i'm a newbie. It's possibile in a portlet realize a uplod file? How can i do? Help me there isn't an example on the web. Thank you

        • 1. Re: upload file
          peterj

          Yes you should be able to. The CMS admin portlet has the ability to upload files - take a look at that code to see how it does it.

          • 2. Re: upload file
            raffaele.letizia

            i have problem because that code is almost difficult and i don't know what do the library....help me Peter...

            • 3. Re: upload file
              peterj

              First things first - have you tried doing file upload using a simple servlet? If not, I suggest you try that first. Use the common file upload library, it provides a simply api and does most of the work for you.

              http://commons.apache.org/fileupload/

              Once you have that working, you can start to port your code to a portlet.

              • 4. Re: upload file
                raffaele.letizia

                Peter, i try to realize a upload with the uploadbean:
                http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html

                Uploadbean is a servlet but when i realize in a portlet there is a problem because doesn't function

                • 5. Re: upload file
                  peterj

                  Will uploadbean let you set the action URL? In a portlet, if you want input to come back to your portlet, you need to build the URL the method RenderResponse.createActionURL.

                  • 6. Re: upload file
                    raffaele.letizia

                    the code for addadmin.jsp is there:
                    <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

                    <portlet:defineObjects/>



                    Domanda



                    Risposta



                    Seleziona un file da upload :






















                    my portlet faqportlet is:

                    package com.test;
                    import org.hibernate.Criteria;
                    import org.hibernate.Session;
                    import org.hibernate.SessionFactory;
                    import org.hibernate.Transaction;
                    import org.hibernate.cfg.Configuration;
                    import org.hibernate.criterion.Expression;



                    import javax.portlet.PortletSession;
                    import javax.portlet.ActionRequest;
                    import javax.portlet.ActionResponse;
                    import javax.portlet.GenericPortlet;
                    import javax.portlet.PortletException;
                    import javax.portlet.PortletRequestDispatcher;
                    import javax.portlet.PortletSecurityException;
                    import javax.portlet.RenderRequest;
                    import javax.portlet.RenderResponse;
                    import java.io.File;
                    import java.io.IOException;
                    import java.util.List;



                    public class FaqPortlet extends GenericPortlet
                    {
                    private SessionFactory sessionFactory;



                    public void init()
                    {
                    Configuration config = new Configuration();
                    config.configure("hibernate.cfg.xml");
                    this.sessionFactory = config.buildSessionFactory();
                    }

                    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException
                    {


                    String op = request.getParameter("op");
                    if((op != null) && (op.trim().length() > 0))
                    {
                    String key = request.getParameter("key");
                    String domanda = request.getParameter("domanda");
                    String risposta = request.getParameter("risposta");
                    String filename = request.getParameter("FILENAME");


                    if(op.equalsIgnoreCase("Aggiungi FAQ"))
                    {
                    addNewContact(request.getUserPrincipal().getName(),domanda,risposta,filename);
                    }
                    else if(op.equalsIgnoreCase("Aggiorna FAQ"))
                    {
                    saveContact(key,domanda,risposta,filename);
                    }
                    else if(op.equalsIgnoreCase("Cancella FAQ"))
                    {
                    deleteContact(key);
                    }
                    }
                    }

                    public void doView(RenderRequest request, RenderResponse response)
                    {
                    try
                    {
                    if((request.getUserPrincipal() != null) && (request.getUserPrincipal().getName() != null))
                    {
                    String userName = request.getUserPrincipal().getName();
                    String jspName = null;
                    /*verifica per l'admin che ha il controllo totale */
                    System.out.println(userName);



                    if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("Ricerca")))
                    {
                    if (userName.equals("admin"))
                    {
                    request.setAttribute("contacts", getUsersContacts(userName, request.getParameter("domanda")));
                    jspName ="/WEB-INF/jsp/listadmin.jsp";
                    }
                    else
                    {
                    request.setAttribute("contacts", getUsersContacts(userName, request.getParameter("domanda")));
                    jspName ="/WEB-INF/jsp/listuser.jsp";
                    }
                    }
                    else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("shownew")))
                    {
                    if (userName.equals("admin"))
                    {
                    jspName ="/WEB-INF/jsp/addadmin.jsp";
                    }
                    else
                    {
                    jspName ="/WEB-INF/jsp/adduser.jsp";
                    }}

                    else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("edit")))
                    {
                    if (userName.equals("admin"))
                    {
                    request.setAttribute("contact", findContact(userName, request.getParameter("key")));
                    jspName ="/WEB-INF/jsp/editadmin.jsp";
                    }

                    }
                    else if((request.getParameter("op") != null) && (request.getParameter("op").equalsIgnoreCase("delete")))
                    {
                    if (userName.equals("admin"))
                    {
                    request.setAttribute("contact", findContact(userName, request.getParameter("key")));
                    jspName ="/WEB-INF/jsp/deleteadmin.jsp";
                    }
                    }
                    else
                    { if (userName.equals("admin"))
                    {
                    request.setAttribute("contacts", getUsersContacts(userName));
                    jspName ="/WEB-INF/jsp/listadmin.jsp";
                    }
                    else
                    {
                    request.setAttribute("contacts", getUsersContacts(userName));
                    jspName ="/WEB-INF/jsp/listuser.jsp";
                    }
                    }



                    response.setContentType("text/html");
                    PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher(jspName);
                    prd.include(request, response);
                    }
                    else
                    {
                    response.setContentType("text/html");
                    PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/notloggedin.jsp");
                    prd.include(request, response);
                    }
                    }
                    catch(Exception e)
                    {
                    e.printStackTrace();
                    }
                    }

                    private List getUsersContacts(String userName)
                    {
                    Session session = this.sessionFactory.openSession();
                    Criteria criteria = session.createCriteria(Faq.class);
                    //criteria.add(Expression.eq("userName", userName));
                    List contacts = criteria.list();
                    session.disconnect();
                    return contacts;
                    }

                    private List getUsersContacts(String userName, String name)
                    {
                    Session session = this.sessionFactory.openSession();
                    Criteria criteria = session.createCriteria(Faq.class);
                    //criteria.add(Expression.eq("userName", userName));
                    //criteria.add(Expression.like("domanda", name, MatchMode.ANYWHERE));
                    List contacts = criteria.list();
                    session.disconnect();
                    return contacts;
                    }

                    private void addNewContact(String userName, String domanda1, String risposta1,String filename1)
                    {
                    Session session = this.sessionFactory.openSession();
                    Transaction t = session.beginTransaction();
                    try
                    {
                    Faq faq = new Faq();
                    faq.setDomanda(domanda1);
                    faq.setRisposta(risposta1);
                    faq.setUserName(userName);
                    faq.setFilename(filename1);
                    session.save(faq);
                    t.commit();
                    }
                    catch(Exception e)
                    {
                    t.rollback();
                    }
                    finally
                    {
                    if(session != null)
                    {
                    session.disconnect();
                    }
                    }
                    }

                    private Faq findContact(String userName, String id)
                    {
                    Session session = this.sessionFactory.openSession();
                    Criteria criteria = session.createCriteria(Faq.class);
                    //criteria.add(Expression.eq("userName", userName));
                    criteria.add(Expression.eq("key", new Long(id)));
                    List contacts = criteria.list();
                    session.disconnect();
                    if(contacts.size() == 1)
                    {
                    return (Faq) contacts.get(0);
                    }
                    return null;
                    }

                    private void saveContact(String key, String domanda2, String risposta2,String filename2)
                    {
                    Session session = this.sessionFactory.openSession();
                    Transaction t = session.beginTransaction();
                    try
                    {
                    Faq faq = (Faq) session.load(Faq.class, new Long(key));
                    faq.setDomanda(domanda2);
                    faq.setRisposta(risposta2);
                    faq.setFilename(filename2);
                    session.update(faq);
                    t.commit();
                    }
                    catch(Exception e)
                    {
                    t.rollback();
                    }
                    finally
                    {
                    if(session != null)
                    {
                    session.disconnect();
                    }
                    }
                    }


                    private void deleteContact(String key)
                    {
                    Session session = this.sessionFactory.openSession();
                    Transaction t = session.beginTransaction();
                    try
                    {
                    Faq contact = (Faq) session.load(Faq.class, new Long(key));

                    session.delete(contact);
                    t.commit();
                    }
                    catch(Exception e)
                    {
                    t.rollback();
                    }
                    finally
                    {
                    if(session != null)
                    {
                    session.disconnect();
                    }
                    }
                    }
                    }


                    where do you put the createactionURL in my code for result that is ok? can i send my code on your email?

                    • 7. Re: upload file
                      peterj

                      First, when posting XML text or source code, please enclose the text in UBBCode "code" tags - you can do this by selecting the text and clicking the Code button above the editor window. Also, click the Preview button to ensure that the formatting is correct and the XML text shows up before posting.

                      Second, you should consider hiring a consultant to do this for you.