JSP - 无法将图像上传到Google Appengine

时间:2014-10-24 11:48:47

标签: java jsp google-app-engine servlets

我想将图片上传到我的appengine 因此,我有了诱惑MyImage(来自互联网上的另一个主题):

@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;
    }
}

我有这个代码(也来自internt): (它是jsp页面使用的servlet。)

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();
            }
        }
    }
}

当我检查数据源查看器时,我可以看到创建了MyImage类型的对象 但没有合适的内容。 (名称字段为空)

我认为从jsp文件获取信息时遇到问题如下:

<%@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>

servlet声明:

<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>

任何想法??

1 个答案:

答案 0 :(得分:1)

按照Blobstore Java API Overview上的示例上传您的图片。它比您在代码中尝试的操作容易得多。