gpt4 book ai didi

image - 在 Servlet 中获取一组图像并将它们发送到我的 Android 应用程序

转载 作者:可可西里 更新时间:2023-11-01 17:36:24 24 4
gpt4 key购买 nike

我正在关注 this教程,其中解释了如何通过将图像声明为 Blob 来将图像存储到数据存储中目的。没关系,问题在于示例中的图像是这样获取的:

public class StoreMovieServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
// Fetch the image at the location given by the url query string parameter
HTTPResponse fetchResponse = fetchService.fetch(new URL(req.getParameter("url")));

String fetchResponseContentType = null;
for (HTTPHeader header : fetchResponse.getHeaders()) {
// For each request header, check whether the name equals
// "Content-Type"; if so, store the value of this header
// in a member variable
if (header.getName().equalsIgnoreCase("content-type")) {
fetchResponseContentType = header.getValue();
break;
}
}
if (fetchResponseContentType != null) {
// Create a new Movie instance
Movie movie = new Movie();
movie.setTitle(req.getParameter("title"));
movie.setImageType(fetchResponseContentType);

// Set the movie's promotional image by passing in the bytes pulled
// from the image fetched via the URL Fetch service
movie.setImage(fetchResponse.getContent());

PersistenceManager pm = PMF.get().getPersistenceManager();
try {
// Store the image in App Engine's datastore
pm.makePersistent(movie);
} finally {
pm.close();
}
}
}
}

而且我没有 url从哪里获取图像。相反,我想将它们存储在我的 Servlet 中的某个位置。然后从那里将它们存储到数据存储中。

因此,我怎样才能获取一些存储在我的 Servlet 本地的图像,而不是从外部 url 获取它们?

换句话说,我应该将要转换的图片放在哪里?

UPDATE

As suggested by @Nick, I realized that the the tutorial in question isn't the good solution to my needs. Thus I've changed it all over again and did so, within the doPost method of my Servlet:

InputStream s = this.getClass().getClassLoader().getResourceAsStream("http://cardimgs.org/imgs/cardbackground1.jpg");

Movie movie = new Movie();
try {
byte[] bytes = IOUtils.toByteArray(s);
movie.setImage(bytes);

}catch (IOException e){
e.printStackTrace();
}

PersistenceManager pm = PMF.get().getPersistenceManager();
try {
// Store the image in App Engine's datastore
pm.makePersistent(movie);
} finally {
pm.close();
}

Movie类(class): link ,

和我的 appengine-web.xml : link 1

我尝试定义我的静态文件的地方。但是,问题是 InputStream 上面的代码似乎是 null并返回 NullPointerException .以下是我在 Servlet 中存储图像的方式:

link 2

如何才能成功访问我的静态文件?

UPDATE 2

I was able to load images by changing this:

InputStream s = this.getClass().getClassLoader().getResourceAsStream("http://cardimgs.org/imgs/cardbackground1.jpg");

为此:

InputStream s = getServletContext().getResourceAsStream("/WEB-INF/imgs/cardbackground1.jpg");

无需添加属性为<include>的文件夹路径在appengine-web.xml文件。

最佳答案

粗略浏览本教程并不是处理此类内容的好方法。它看起来是一个旧教程。

要在 gae 中存储图像,您应该使用图像服务。它将为您处理所有基础知识,并为您提供一个 URL 以在网页上呈现内容。它还可以处理动态调整大小和裁剪,以及通过 google 基础设施提供服务。

要解决您的表面问题,要从您的 Java 项目加载文件,您可以使用类加载器。

this.class.getClassloader().getResourceAsStream("image/resource.jpg")

但是,您也可以直接在 GAE 上提供 war 中的静态资源,这同样将由 google 的基础设施管理,并且是首选方法。这是在 appengine-web.xml 文件中配置的。根据您的描述,我认为这是您应该做的。

关于image - 在 Servlet 中获取一组图像并将它们发送到我的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30287688/

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