html - 使用多部分/表单数据时如何从请求中获取字符串参数?

标签 html google-app-engine servlets file-upload servlet-filters

<分区>

我制作了 html 页面来上传带有文本框的图像,文本框包含它的描述。 我使用了 multipart/form-data ;在 servlet 的 dopost 中,我使用 ServletFileUpload upload = new ServletFileUpload();

为了获取字符串参数,我使用了 request.getparameter(); 但它总是给我 NULL ??? 我怎样才能得到它??

html::

<form name="filesForm" action="/upload" method="post" enctype="multipart/form-data">

File : <input type="file" name="file">  
<textarea name = "description"  
 rows = "4" cols = "30">Enter comments here.</textarea>
<input type="submit" name="Submit" value="Upload File">

在小服务程序上:

ServletFileUpload upload = new ServletFileUpload(); 
upload.setSizeMax(500000000);
 FileItemIterator iterator = null;
    try {
        iterator = upload.getItemIterator(req);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } //  to handle contents or 
    // instances  of request.
    FileItemStream item = null;
    try {
        item = iterator.next();
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // get  access each item on iterator.
    java.io.InputStream in = item.openStream(); // allows to read the items contents. 


    Blob imageBlob = new Blob(IOUtils.toByteArray(in)); // store each item on Blob 


    // object after converting them to Bytes.
    /*……….
    Note: we are concerned on uploading images files. So the type of files is either (.jpg or gif)
    ……….
    */
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String counter="1";
    ServletOutputStream outt = resp.getOutputStream();
     //match incoming request Content type and invoke appropriate method for handel request

最佳答案

这是因为当你有表单 enctype="multipart/form-data" 时。您无法使用 request.getParameter("paramnName"); 获取其他表单字段。它总是会给你 NULL。

您必须使用 FormItemisFormField() 来检查其常规字段或文件。

例子:

        try {
            ServletFileUpload upload = new ServletFileUpload();
            response.setContentType("text/plain"); 

            FileItemIterator iterator = upload.getItemIterator(request);

            while (iterator.hasNext()) {
              FileItemStream item = iterator.next();

              InputStream stream = item.openStream();

              if (item.isFormField()) {
                  System.out.println("Got a form field: " + item.getFieldName()  + " " +item);

              } else{
                  System.out.println("Got an uploaded file: " + item.getFieldName() +
                          ", name = " + item.getName());
                int len;
                byte[] buffer = new byte[8192];
                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {

                  response.getOutputStream().write(buffer, 0, len);

                }

            }

        }
} catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

关于html - 使用多部分/表单数据时如何从请求中获取字符串参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10379013/

相关文章:

jquery - CSS 在溢出设置为隐藏时删除水平滚动

html - 鼠标悬停在菜单按钮外显示菜单

html - 用自定义符号替换列表元素符号(右对齐)

javascript - Can (Html5,javascript,css3) 应用程序将在 ios 和 android 上完美运行

java - Google App Engine 用户代理

python - 如何使用 Cloud Storage JSON API 检索超过 64KB 的文件?

java - 为 GAE maven 项目添加 java 方面

java - 当我调用 from 方法时,servlet 不会调用

java - 将两个不同的 servlet 映射到相同的 URL 模式

java - 更新 session 数据的正确语义