gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:22:51 27 4
gpt4 key购买 nike

我想将图像上传到我的应用程序引擎因此我有实体 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>

有什么想法吗?

最佳答案

按照Blobstore Java API Overview上的示例进行操作上传您的图像。这比您在代码中尝试执行的操作要容易得多。

关于java - JSP - 无法将图像上传到 Google Appengine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com