【问题标题】:How to submit Jsp Page with encoding multipart/form-data如何使用编码多部分/表单数据提交 Jsp 页面
【发布时间】:2011-01-30 00:55:31
【问题描述】:

我正在提交带有编码的 html 表单 (multipart/form-data) 我在 Jsp 页面中有以下字段 名 姓 要上传的文件名

文件已完美上传 如何获得名字和姓氏? 我想保存在数据库中。

【问题讨论】:

    标签: jsp servlets file-upload


    【解决方案1】:

    您需要使用与获取文件内容相同的 API 来提取文本字段。假设您为此使用(事实上的标准)Apache Commons FileUpload,那么您需要在FileItem#isFormField() 返回true 时采取行动。

    try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();
                // ... (do your job here)
            } else {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = FilenameUtils.getName(item.getName());
                InputStream filecontent = item.getInputStream();
                // ... (do your job here)
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }
    

    另见:

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2013-03-06
      • 2012-06-16
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2016-10-27
      • 2012-12-15
      相关资源
      最近更新 更多