user3071790
user3071790

Reputation: 43

JSP - Can't upload image to Google Appengine

i want to upload a image to my appengine therefor i have the entite MyImage(from another sourse in the internet):

@Entity
public class MyImage implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Id 
    private Long id;

    static int ID=1;


    private String name;


    Blob image;

    public MyImage()
    {
        id=(long) ID++;
    }

    public MyImage(String name, Blob image)
    {
        this.name = name;
        this.image = image;
        id=(long) ID++;
    }

    // JPA getters and setters and empty contructor
    // ...
    public Blob getImage()
    {
        return image;
    }

    public void setImage(Blob image)
    {
        this.image = image;
    }
}

i have this code(also from internt): (it's servlet that used by the jsp page.)

public class UploadImg extends HttpServlet
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    {
        // Get the image representation

        try
        {
            HelpDebug.msg="start of servlet";
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iter = upload.getItemIterator(req);
            FileItemStream imageItem = iter.next();
            InputStream imgStream = imageItem.openStream();
            HelpDebug.msg="after open stream";
            // construct our entity objects

            Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
            MyImage myImage = new MyImage(imageItem.getName(), imageBlob);

            HelpDebug.msg="after creating Image";
            // persist image
            BackendFactory.getInstance().SendImg(myImage);
            HelpDebug.msg="after get instance ";
            // respond to query
            res.setContentType("text/plain");
            res.getOutputStream().write("OK!".getBytes());
        } catch (Exception ex)
        {
            try
            {
                res.getOutputStream().write(ex.getMessage().getBytes());
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

when i check the datasource viewer i can see that an object from the type MyImage was created but not with the right content. (the name field there is null)

i think i have a problem with getting the information from the jsp file as follows:

<%@page import="com.example.servlets.HelpDebug"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
    content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>


    <form action="UploadImg" method="post" enctype="multipart/form-data">

        <input name="name" type="text" value=""> <br /> <input
            name="imageField" type="file" size="30"> <br /> <input
            name="Submit" type="submit" value="Sumbit">
        <%
            HelpDebug.desc = request.getParameter("name") == null ? "null :P"
                    : request.getParameter("name");
            out.println(HelpDebug.getMsg());
        %>
    </form>


</body>
</html>

the servlet declartion:

<servlet>
        <servlet-name>UploadImg</servlet-name>
        <servlet-class>com.example.servlets.UploadImg</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>UploadImg</servlet-name>
        <url-pattern>/UploadImg</url-pattern>
    </servlet-mapping>

any ideas??

Upvotes: 0

Views: 147

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

Follow the examples on Blobstore Java API Overview to upload your images. It's much easier than what you are trying to do in your code.

Upvotes: 1

Related Questions