Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java EE - Uploading image to database

Options
  • 21-05-2013 11:42am
    #1
    Registered Users Posts: 8,324 ✭✭✭


    I'm doing a site for someone, and they want to be able to add items to the site themselves, which is fine. However, they want to be able to up load an image, and I'm hitting a block. I've looked into it, and I've decided to just upload the image to the database (as it's going to be, at most, about 300 items in the database at any one time so not going to be thousands at any stage).

    Tech wise, I am using Java EE with glassfish and JDBC/derby for the database. I am using entity class mapping. See the class in question below

    Clothes class
    public class Clothes implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "ID")
        private Integer id;
        @Size(max = 255)
        @Column(name = "DESIGNER")
        private String designer;
        @Size(max = 255)
        @Column(name = "CNAME")
        private String cname;
        @Size(max = 20)
        @Column(name = "CTYPE")
        private String ctype;
        @Size(max = 1024)
        @Column(name = "DESCRIPT")
        private String descript;
        @Lob
        @Column(name = "IMAGE")
        private byte[] image;
    
        public Clothes() {
        }
    

    I know the column needs to be of type blob, which is done. Here's my partial code so for, which is working fine for uploading to the database, just setting the image to null at the moment.

    Clothes Bean
    @Override
        public void addClothes(String designer, String cname, String ctype, String desc) {
            Clothes c = new Clothes();
            em.persist(c);
            c.setDesigner(designer);
            c.setCname(cname);
            c.setCtype(ctype);
            c.setDescript(desc);
            c.setImage(null);
        }
    

    Servlet Code
     @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            clothesBean.addClothes(request.getParameter("designer"), request.getParameter("cname"), request.getParameter("ctype"), request.getParameter("desc"));
            out = response.getWriter();
            Design.htmlHeader(out);
            out.println("<h1>Added</h1>");
            Design.htmlLinks(out);
            Design.htmlCloser(out);
        }
    

    My main queries are

    1) How do I upload the image to the database?

    2) How do I get it back out to display it in a webpage?

    Thanks!


Comments

  • Registered Users Posts: 40 razor2013


    I think you can use @MultipartConfig annotation and Part interface

    Have a look:

    http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    Right, I had a look and have modified code. It *seems* to be working, in that it runs successfully and everything seems dandy, only that the directory is not created (or if it is, it's not saving where I think it is, a more than likely scenario).

    I am assuming it should create in the Project-war/web folder (where the jsp page is) or the Project-war/src/java/admin folder where the servlet is called from, but it's not there.
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            out = response.getWriter();
            final String path = "images" + File.separator + request.getParameter("designer") + File.separator + request.getParameter("ctype") + File.separator + request.getParameter("cname");
            out.println(path);
            final Part filePart = request.getPart("image");
            final String filename = "image.jpg";
            File file = new File(path);
            if (!file.exists()){
                out.println("Dir Doesn't Exist");
                file.mkdirs();
            }
            OutputStream outstream = null;
            InputStream filestream = null;
            
            try{
             outstream = new FileOutputStream(new File(path + File.separator + filename));
             filestream = filePart.getInputStream();
             int read = 0;
             final byte[] bytes = new byte[1024];
             while(( read = filestream.read(bytes)) != -1){
                 outstream.write(bytes, 0, read);
             }
             out.println("New file " + filename + " created at " + path);
            }catch (FileNotFoundException fne) {
            out.println("You either did not specify a file to upload or are "
                    + "trying to upload a file to a protected or nonexistent "
                    + "location.");
            out.println("<br/> ERROR: " + fne.getMessage());
            }finally {
            if (out != null) {
                out.close();
            }
            if (filestream != null) {
                filestream.close();
            }
            if (outstream != null) {
                outstream.close();
            }
        }
         
        }
    

    EDIT:

    Yeah, it's definitely the file path. Tried created to c:\images and it worked fine. Any ideas what file path I should get using? Want to keep it relative, obviously!

    EDIT AGAIN:

    And it's at C:\Users\Chris\AppData\Roaming\NetBeans\7.2.1\config\GF3\domain1\

    Any idea how to set it to the actual root directory for the index.jsp file?


Advertisement