java JSP多部分请求时获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13321164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:17:31  来源:igfitidea点击:

Get parameter when multipart request in JSP

javajspservlets

提问by alaa joma

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Add new product</title>
            <link type="text/css" rel="Stylesheet" href="css/SiteStyle.css" />
            <script src="js/jquery-1.3.2.js" type="text/javascript"></script>
    </head>
    <body>
         <form id="MainForm" action="Relay" method="post" enctype="multipart/form-data">
            <input name="destination"  type="hidden" value="AddNewProduct" />
             <script src="js/AddNewProduct.js" type="text/javascript"></script>
            <center>
                <h1>Add new product</h1>
            <hr />
        <table class="TableLogin">
            <tr>
                <td align="center" colspan="2" class="TDLoginHeader">
                        Product information</td>
            </tr>
            <tr>
                <td align="right" class="TDLoginTitle">
                        Product Title
                </td>
                <td align="left" class="TDLoginText">
                    <input id="TxtTitle" type="text" class="LoginTextBoxes" name="Title" maxlength="75" /></td>
            </tr>
            <tr>
                <td align="right" class="TDLoginTitle">
                        Dollar price
                </td>
                <td align="left" class="TDLoginText">
                    <input id="TxtPrice" type="text"  class="LoginTextBoxes" name="Price" maxlength="15" /></td>
            </tr>
            <tr>
                <td align="center" colspan="2" class="TDLoginTitle">
                    Product number</td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <table style="width:100%;">
            <tr>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit1' name='Txtdigit1' class='TxtProductNumber' type='text' />
               </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit2' name='Txtdigit2' class='TxtProductNumber' type='text' />
                </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit3' name='Txtdigit3' class='TxtProductNumber' type='text' />
                </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit4' name='Txtdigit4' class='TxtProductNumber' type='text' />
                </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit5' name='Txtdigit5' class='TxtProductNumber' type='text' />
                </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigi6' name='Txtdigit6' class='TxtProductNumber' type='text' />
                </td>
                <td align='center' class='TDProductNumber'>
                    <input id='Txtdigit7' name='Txtdigit7' class='TxtProductNumber' type='text' />
                </td>
            </tr>
            <tr>
                <td colspan="7" class="TDLoginTitle">
                    <label>Product Image</label>
                        <input id="ProductImage" name="ProductImage" type="file" />
                </td>
            </tr>
          </table>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                      <input id="BtnAdd" type="button" value="Add" class="BtnSize" /></td>
            </tr>
        </table>
        <div id="DivAddMessage">
        </div>
        <div>
           <a href='Login.jsp'>Back</a>
        </div>
        </center>
        </form>
    </body>
</html>

Servlet 'Relay' code:

Servlet“中继”代码:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();            
    String destination=request.getParameter("destination");
    out.print(destination);
}

this is a piece of code for enter product inforamtion (title , price , 7 digit number, product image) , when i want to get 'destination' parameter in servlet the value got is null i know that because multipart request , but how i can get this value and the file uploaded value ?

这是一段用于输入产品信息(标题、价格、7 位数字、产品图片)的代码,当我想在 servlet 中获取“目标”参数时,得到的值为 null 我知道这是因为多部分请求,但我怎么能获取此值和文件上传值?

回答by Sathish

Since you post using multipart encoding ('multipart/form-data') the parameters are not present as you expect.

由于您使用多部分编码 ('multipart/form-data') 进行发布,因此参数并不如您所愿。

If for instance you are using commons-fileuploadthe parameters would be present as and are identifiable using the 'isFormField' method on the FileItem object.

例如,如果您使用的是commons-fileupload,则参数将显示为并且可以使用 FileItem 对象上的“isFormField”方法进行识别。

This thead on coderanch explains how: coderanch

coderanch 上的这个标题解释了如何:coderanch

Most (every) modern webframeworks abstract this away and make this sort of stuff much easier by the way. Refer this site it will help you

大多数(每一个)现代网络框架都将其抽象出来,顺便让这类东西变得更容易。 参考这个网站它会帮助你

CODE

代码

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);
    }

    // ...
}

回答by laksys

use Annotation

使用注解

@MultipartConfig

@MultipartConfig

for your Servlet

为您的 Servlet