多部分/表单数据如何隐藏参数 [英] Multipart/form-data how to get parameter hidden

查看:102
本文介绍了多部分/表单数据如何隐藏参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Web应用程序.我的jsp带有enctype ="multipart/form-data",提交请求时,无法在servlet中获取请求参数.

I 'm working on a web application. I have my jsp with enctype="multipart/form-data" and when I submit my request, I am unable to get the request parameters in servlet.

getParameter()调用将全部返回null.问题是如何克服这个问题?

The getParameter() calls will all return null. The question is how can overcome this problem?

当未加密时,此代码可以正常工作.我知道这个问题已经问过很多遍了,但是我没有找到直接的答案

When it 's not enctyped, this code works fine. I know that this has been asked many times, but I did not find any straight answer

JSP

<form action="upload" method="post" enctype="multipart/form-data">
                <input type="file" name="uploadfile[]" id="uploadfile" size="50" multiple="true" />
                <br/><br/>
                <input type="hidden" name ="e_id" value= <%=userBean.getEid%> />
                <input type="hidden" name ="Uid" value= <%=userBean.getUid()%> />
                <input type="submit" name ="button1" value="Upload" />
            </form>

Servlet

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{

    int e_id =0;
    String uid = null;

    HttpSession session1 = request.getSession(true);    
    if(ServletFileUpload.isMultipartContent(request)){//process only if its multipart content
        try 
        {
          List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
          for(FileItem item : multiparts)
          {
              if(!item.isFormField())
              {
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));

                    e_id = Integer.parseInt(request.getParameter("e_id"));
                    uid = request.getParameter("Uid");
              }
              else {}
...

推荐答案

您需要使用@MultipartConfig注释servlet,并获取所用参数的值:

You need to annotated your servlet with the @MultipartConfig and to get the value of the parameters you use:

Part idPart = req.getPart("e_id");
try (Scanner scanner = new Scanner(idPart.getInputStream())) {
    String idValue = idPart.nextLine(); // read from the part
} 

我有一个项目以及如何使用它的示例:

I have a project in github with an example of how to use it:

  • Servlet
  • Upload page

这篇关于多部分/表单数据如何隐藏参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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