App-Engine(Java)文件上传

时间:2010-04-26 08:31:13

标签: java google-app-engine file upload

我设法使用以下示例在App-Engine上传文件:

How to upload and store an image with google app engine (java)

How to upload pics in appengine java

问题是,我正在提交其他字段以及下面列出的文件字段:

<form action="index.jsp" 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">
</form>

在我的servlet中,查询时我得到null

name = request.getParameter("name");

为什么会这样?有没有办法检索文本字段值?

1 个答案:

答案 0 :(得分:9)

您必须通过FileItemIterator。在您提到的示例中,仅处理图像(FileItemStream imageItm = iter.next();)。

// From the example: http://stackoverflow.com/questions/1513603/how-to-upload-and-store-an-image-with-google-app-engine-java
FileItemIterator iter = upload.getItemIterator(req);
// Parse the request
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.openStream();
    if (item.isFormField()) {
        System.out.println("Form field " + name + " with value "
            + Streams.asString(stream) + " detected.");
    } else {
        // Image here.
        System.out.println("File field " + name + " with file name "
            + item.getName() + " detected.");
        // Process the input stream
        ...
    }
}

有关详细信息,请参阅http://www.jguru.com/faq/view.jsp?EID=1045507