【问题标题】:Google App Engine - Blobstore request working on production not in developmentGoogle App Engine - Blobstore 请求在生产中工作而不是在开发中
【发布时间】:2012-03-21 08:07:56
【问题描述】:

我正在使用谷歌应用引擎开发上传表单来上传 csv 文件。 我目前遇到的问题是,在开发环境中,请求操作将被正确发送。

在生产环境中,请求操作似乎有效,尽管两种形式的操作相同。

例如

发展

   action="http://localhost:8080/_ah/upload/ag50cy1zY2gtcmVwb3J0c3IbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGAEM"

制作动作:

    action="http://website.appspot.com/_ah/upload/AMmfu6Yer2BJaT_tW_fmc- PKvHaOHD3pnv5QH6o6d8XQQujbCWg5egbjf2sGxP5_cN6uAyvgDVOn8U40wLLXEvoQcrMDbHQQByJpTlamzBPz_8x8LN2UWKM/ALBNUaYAAAAAT1EmxMkvj7tiS9WAvYAWKPG1sN1DvmMk/"

当我在日志中进行调查时,开发服务器指出该操作是: "/ag50cy1zY2gtcmVwb3J0c3IbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGAEM"

如生产日志所述,该操作是: “/form”(如预期的那样)

两者的代码在下面是相同的。

文件:upload.jsp

    <%
               request.getAttribute("message");

                BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
            %>

            <hr/>
            <p>Upload a .csv file to the system.</p>
            <fieldset>
                <legend>Upload File</legend>
                <form action="<%= blobstoreService.createUploadUrl("/upload/form") %>" method="post" enctype="multipart/form-data">
                    <label for="filename_1">File: </label>
                    <input id="filename_1" type="file" id="filename_1" name="file" size="30" onchange="checkInput();" /><br/>
                    <br/>
                    <input type="submit" id="submitBtn" value="Upload File" />
                </form>
            </fieldset>

文件:controller.java

    else if (action.equals("/form")) {
            Logger.getLogger(Controller3.class.getName()).log(Level.INFO, action);

            Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
            List<BlobKey> bkList = blobs.get("filename_1");
            BlobKey blobKey = bkList.get(0);

            if (blobKey == null) {
                Logger.getLogger(Controller3.class.getName()).log(Level.WARNING, "Blob null");
                response.sendRedirect("/");
            } else {
                Logger.getLogger(Controller3.class.getName()).log(Level.WARNING, "Blob not null");
                FileService fileService = FileServiceFactory.getFileService();

                // Create a new Blob file with mime-type "text/plain"
                AppEngineFile file = fileService.getBlobFile(blobKey);// Again, different standard Java ways of reading from the channel.
                FileReadChannel readChannel = fileService.openReadChannel(file, false);
                Reader reader1 = new BufferedReader(Channels.newReader(readChannel, "UTF8"));

                char c = ',';
                CSVReader reader = new CSVReader(reader1, c);

                ArrayList<ArrayList<String>> results = reader.getResults();
                ArrayList<String> columns = reader.getColumns();

                request.setAttribute("maxColumns", columns);
                request.setAttribute("csvResults", results);

                //Remove the CSV file from the blobstore now we have used it.
                blobstoreService.delete(blobKey);
                this.getServletContext().setAttribute("csvUploadResults", results);
            }

已设置 web.xml 来处理请求: /_ah/upload/, /upload/, /_ah/upload/upload/*

我希望有一个简单的解释(可能是)为什么代码在生产中有效但在开发中无效。

任何帮助都会有很大帮助。

【问题讨论】:

    标签: java google-app-engine jsp blob blobstore


    【解决方案1】:

    我目前也在与 Blobstore 搏斗。

    我注意到你说过: web.xml 已设置为处理请求:/_ah/upload/、/upload/、/_ah/upload/upload/*

    但请注意,在 blobstore 处理了添加 blob 的请求之后(通过发布到您获得 createUploadUrl("/upload") 的 URL)然后它将调用映射到 web.xml 中“/upload”的您的方法。因此,您肯定要删除对 /_ah/upload/ 的任何 web.xml 引用,因为这是 blobstore 将拦截的内容。

    我这样做了一段时间,得到一个服务器错误 500,没有任何日志记录。

    【讨论】:

    • 我找到了解决问题的方法。我照你说的做了,从 web.xml 中删除了 /_ah/*。我还创建了一个新的 servlet 来处理上传功能。我不太确定这有什么帮助,但系统现在按预期工作。
    • 太棒了,很高兴听到它有帮助。(我可以给我一个信用吗;)几天前我也做了同样的事情。顺便说一下,对于其他在这个 API 上苦苦挣扎的人,我发现了另一个大问题。为了向您的应用程序传递其他参数,例如图像名称或图像 ID(如果您在应用程序中保存对图像的引用),您需要将它们作为表单中的表单字段传递。但是,当您在上传方法中阅读此内容时,它们会被翻译成参数。令人讨厌的是,开发环境足够聪明,可以同时读取表单字段和参数,而在生产环境中并非如此。
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2012-01-11
    • 2014-07-23
    相关资源
    最近更新 更多