0

我正在学习如何使用 Java 和 Google App Engine 进行编码。我想检索一些我上传到 Google App 数据库的图像并显示它们。我的代码可以编译,但图像未显示在浏览器中。我看到的只是一个小广场。

我怀疑该错误之前在 viewimage.jsp 文件中。有人可以看看我的一些 jsp 文件并给我一些建议吗?非常感谢您的帮助。感谢你!

视图图像.jsp

<%@ page import="javax.jdo.*" %>
<%@ page import="com.*" %>
<%
    PersistenceManager pm = PMF.getPMF().getPersistenceManager();
    try {
        String itemId = request.getParameter("item");

        Item item = (Item)pm.getObjectById(Item.class, Long.parseLong(itemId));
        byte[] photo = item.getPhoto(item);
        response.setContentType("image/jpeg");
        response.getOutputStream().write(photo);
    } catch (Exception cannotLoad) {
        cannotLoad.printStackTrace(); 
    } finally {
        pm.close();
    }
%>

视图.jsp

<!DOCTYPE html>
<html><head>
    <title>CS496 Homework 2 - Display Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="jquery-1.10.2.min.js"></script>
    <script>
    var MOBILE = navigator.userAgent.match(
                    /Android|BlackBerry|iPhone|iPad|iPod|IEMobile/i) ? true : false;
    if (MOBILE) { 
        document.write("<link rel=stylesheet href='http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css'>");
        document.write("<link rel=stylesheet href='stylemobile.css'>");
        document.write('<s'+'cript src="jquery.mobile-1.3.2.min.js"></scr'+'ipt>');
    } else
        document.write("<link rel=stylesheet href='styledesktop.css'>");
    </script>

</head><body>

<div data-role="page">
    <div data-role="header">
        <h1>CS496 Homework 2</h1>
    </div>

    <div data-role="content" class="photolist"> 
        <ul data-role="listview" data-inset="true" id="allitems"></ul>
    </div>
</div>
<div data-role="page" id="detailPage">
    <div data-role="header">
        <h1 id="detailTitle"></h1>
    </div>

    <div data-role="content" id="detailContent" class="photoViewer">    
        <img src="" id="detailImage">
    </div>
</div>

<script>
function addItemMobile(itemId, school) {
    var anchor = $("<a/>").text(school).attr('href', '#detailPage').click(function() { 
        $("#detailTitle").text(school);
        $("#detailImage").attr('src', '');
        $("#detailImage").attr('src', 'viewimage.jsp?item='+itemId);
    });
    var li = $("<li/>").append(anchor);
    $("#allitems").append(li);
}

function addItemDesktop(itemId, school) {
    var loc = $("#allitems");
    loc.append($("<h2 />").text(school));
    loc.append($("<div />").append($("<img />").attr('src', 'viewimage.jsp?item='+itemId)));
}

var addItem = MOBILE ? addItemMobile : addItemDesktop;

$.ajax({
    type:"GET",
    dataType:"json",
    url:"viewapi.jsp",
    success:function(items) {
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            addItem(item.id, item.school);
        }
        $("#allitems").listview('refresh');
    },
    error:function(a,b,c) {
        alert("ERROR: "+a);
    }
});

</script>


</body></html>

视图api.jsp

<%@ page import="java.util.*" %>
<%@ page import="javax.jdo.*" %>
<%@ page import="org.json.simple.*" %>
<%@ page import="cs496hwk2.*" %>
<%
    PersistenceManager pm = PMF.getPMF().getPersistenceManager();
    try {
        List<Item> items = Item.loadItems(null, pm);
        JSONArray array = new JSONArray();
        for (Item item : items) {
            JSONObject object = new JSONObject();
            object.put("id", item.getID());
            object.put("school", item.getSchool());
            array.add(object);
        }
        out.write(array.toString());
    } finally {
        pm.close();
    }
%>
4

0 回答 0