如何通过< input type ="文件"传递更多信息? &GT ;? [英] How do I pass additional info with <input type="file" >?

查看:76
本文介绍了如何通过< input type ="文件"传递更多信息? &GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件上传到在tomcat上运行的servlet。除了文件之外,我想让用户添加与上传文件相关的评论。我尝试了以下,但它没有奏效:

 < form action ='MyUploadServlet'enctype ='multipart / form-data 'method ='POST'> 
< input type ='file'name ='filechooser'>< br />
< textarea name ='comment'cols = '15'rows ='5'>< / textarea>
< input type ='Submit'value ='上传'>< br />
< / form>

以下是服务器端代码片段:

  @WebServlet(/ MyUploadServlet)
public class MyUploadServlet扩展HttpServlet {$ b $ protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{
ServletContext sc = request.getServletContext();
String comment =(String)request.getParameter(comment);
.... etc}

服务器代码的省略部分涉及接收上传文件的内容。

上面的request.getParameter(comment)行返回null。
我在我的应用程序中使用了几个servlet和多个表单。在其他所有地方,如果我使用request.getParameter(form-input-name),我总是得到相应输入字段的值。这是唯一的例外,即当表单有一个类型为file的输入字段时。



如何在文件上传提交时传递评论?

解决方案

您需要通过相同 API获取文本字段值,您已指定HTML表单以 multipart / form-data 编码方式发送数据。 而不是标准的 application / x-www-form-urlencoded 编码。为了将文件内容与表单提交一起发送, multipart / form-data 编码是强制性的。但是, getParameter()方法仅与 application / x-www-form-urlencoded 结合使用。



多部分/表单数据请求通常要用 multipart / form- data 解析器,比如众所周知的 Apache Commons FileUpload ,这是一个de这个领域的事实标准。但是,由于Servlet 3.0(您似乎实际使用的是Servlet 3.0中引入了 @WebServlet 注释)中,有一个新的 getParts() 方法,它允许您使用标准方法提取必要的提交数据,而不需要Apache Commons FileUpload。然而,它仍然比Apache Commons FileUpload稍微冗长一些。您可以在此答案中找到两种方法的具体示例:如何使用JSP / Servlet将文件上传到服务器?


I need to upload files to a servlet running on tomcat. In addition to the file, I want to allow the user to add a comment associated with the file uploaded. I tried the following but it did not work:

<form action='MyUploadServlet' enctype='multipart/form-data' method='POST'>
    <input type='file' name='filechooser'><br />
    <textarea name='comment' cols='15' rows='5'></textarea>
    <input type='Submit' value='Upload'><br />
</form>

Here is a snippet from the server side code:

@WebServlet("/MyUploadServlet") 
public class MyUploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {
            ServletContext sc = request.getServletContext();
            String comment = (String)request.getParameter("comment");
            ....etc}

The omitted part of the server code deals with receiving the contents of uploaded file.

The request.getParameter("comment") line above returns null. I use several servlets and multiple forms in my app. In all other places, if I use request.getParameter("form-input-name"), I always get the value of the corresponding input field. This is the only exception, namely when the form has an input field of type "file".

How do I pass in a comment along with the file upload submit?

解决方案

You need to get the text field value by the same API as you used to get the content of the uploaded file.

You have specified the HTML form to send the data in multipart/form-data encoding instead of the standard application/x-www-form-urlencoded encoding. The multipart/form-data encoding is mandatory in order to send the file's content along with the form submit. However, the getParameter() method works in combination with application/x-www-form-urlencoded only.

A multipart/form-data request is usually to be parsed with a multipart/form-data parser such as the well known Apache Commons FileUpload, which is a de facto standard in this area. However, since Servlet 3.0 (which you seem to be actually using, given the presence of the also in Servlet 3.0 introduced @WebServlet annotation), there's a new getParts() method which allows you to extract the necessary submitted data using the standard methods without the need for Apache Commons FileUpload. It's however still only a bit more verbose than with Apache Commons FileUpload. You can find a concrete example of the both approaches in this answer: How to upload files to server using JSP/Servlet?

这篇关于如何通过&lt; input type =&quot;文件&quot;传递更多信息? &GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆