如何将输入值从jsp中的multipart / form-data传递给servlet?

时间:2017-04-03 11:54:41

标签: java jsp servlets multipartform-data

我知道也许它可能是一个重复的问题,我在互联网上阅读类似的帖子,但我不能将我的输入值从jsp发送到servlet。 我可以上传文件,但我需要发送一个输入值。 Servlet的:

try {
                FileItemIterator iterator = upload.getItemIterator(req);
                while (iterator.hasNext()) {
                    FileItemStream item = iterator.next();
                    InputStream in = item.openStream();


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


                            } else {
                                String fieldName = item.getFieldName();
                                String fileName = item.getName();
                                String contentType = item.getContentType();
                                String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);

                                out.println("--------------");
                                out.println("fileName = " + fileName);
                                out.println("Kind = " + fileNameWithOutExt);
                                out.println("field name = " + fieldName);
                                out.println("contentType = " + contentType);

                                String fileContents = null;
                                try {

                                    fileContents = IOUtils.toString(in);
                                    out.println("lenght: " + fileContents.length());

                                    String[] content = fileContents.split("\n");


                                    String[] property = content[0].split(",");

                                    out.println("lunghezza " + property.length);
                                    for (String propr : property) {
                                         out.println("prop: " + propr);
                                     }
                                    //out.println(Arrays.toString(property));
                                    //out.println(fileContents);
                                } finally {
                                    IOUtils.closeQuietly(in);
                                            }

                                    }
                                            }
                } catch (SizeLimitExceededException e) {
                    out.println("You exceeded the maximu size ("
                            + e.getPermittedSize() + ") of the file ("
                            + e.getActualSize() + ")");
                                                    }
            } catch (Exception ex) {

                throw new ServletException(ex);
                                }
                                                            }

这就是我的jsp

    <form action="/upload_kind" method="post"  enctype="multipart/form-data" >
<input type="file" name="file" size="50" />
<input type="text" name="namespace" id="namespace">
<br />
<input type="submit" value="Upload File" />
</form>

我读到了&#34; item.getString()&#34;方法,但它在DiskFileItem类而不是在FileItemStream中,所以我可以看到&#34;命名空间&#34;发送,但我看不到它的价值。 谢谢我的坏英语

1 个答案:

答案 0 :(得分:1)

在重复的链接之后,我能够解决我的问题。我用了这段代码:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    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);
    }

    // ...
}