简体   繁体   English

enctype multipart/form-data 在多数据类型表单中的使用

[英]Usage of enctype multipart/form-data in multi data type form

I have html page which contains 3 file input and 3 text inputs.我有包含 3 个文件输入和 3 个文本输入的 html 页面。 If I use enctype = multipart/form-data in jsp page I am not able to get the test input form fields.如果我在 jsp 页面中使用 enctype = multipart/form-data ,我将无法获得测试输入表单字段。 These values always show null.这些值始终显示为空。 If I remove enctype from post form in jsp, I can get the text field inputs but in this case I cannot upload files.如果我从 jsp 中的 post 表单中删除 enctype,我可以获得文本字段输入,但在这种情况下我无法上传文件。 So my question is is it possible to have multiple input fields with file input and if yes how to get the text input field names??所以我的问题是文件输入是否可以有多个输入字段,如果是,如何获取文本输入字段名称?

Any help on this is appreciated..对此的任何帮助表示赞赏..

Below is the html code下面是html代码

<html>
<body>
<form method="post" action="upload.jsp" enctype="multipart/form-data">
  Office Name: <input type="text" name="officeName" />  <br>
  Doc. Description : <input type="text" name="docDesc" />   <br>
  Document 1 :  <input type="file" name="doc1" />   <br>
  Document 2 :  <input type="file" name="doc2" />   <br>
  Document 3 :  <input type="file" name="doc3" />   <br>
  Remarks : <input type="text" name="remarks" />    <br>

  <br>
    <input type="submit" value="submit" />
</form>

And i am retrieving text and file inputs as我正在检索文本和文件输入

strOffficeName=Request.getParameter("officeName");
strDocDescription=Request.getParameter("docDesc");
strDoc1Path=Request.getParameter("doc1");
strDoc2Path=Request.getParameter("doc2");
strDoc3Path=Request.getParameter("doc3");
strRemarks=Request.getParameter("remarks");

I would take a look at Apache Commons FileUpload .我会看看Apache Commons FileUpload It has a User Guide that explains how to get file uploads from your request.它有一个 用户指南,解释了如何从您的请求中获取文件上传。

The section " Processing the uploaded items " shows an example how to process both file uploads and text inputs. 处理上传的项目”部分显示了如何处理文件上传和文本输入的示例。

Create a class called fileUploader which returns ServletFileUpload object创建一个名为 fileUploader 的类,它返回 ServletFileUpload 对象

private FileUploader()
{

}

public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB) 
{
    if(uploader == null)
    {
        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(1024 * 1024);
        factory.setRepository(new File(tempDir));

        uploader = new ServletFileUpload(factory);

        uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
    }

    return uploader;
}

Now you can process a request and read all the data.现在您可以处理请求并读取所有数据。 It handles the files uploaded as well as other form data.它处理上传的文件以及其他表单数据。

protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
throws FileSizeLimitExceededException
{
    if(!isMultipartRequest(request))
        return null;

    ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
    MultiPartFormData data = new MultiPartFormData();
    try
    {
        List<FileItem> items = upload.parseRequest(request);

        for (FileItem item : items) 
        {
            if(item.isFormField())
            {
                data.getParameters().put(item.getFieldName(), item.getString());
            }
            else
            {
                String filename = item.getName();

                //Internet explorer and firefox will send the file name differently
                //Internet explorer will send the entire path to the file name including 
                //the backslash characters etc ... we should strip it down
                //THIS IS HACKY
                if(filename.indexOf("\\") != -1)
                {
                    int index = filename.lastIndexOf("\\");
                    filename = filename.substring(index + 1);
                }


                if(filename == null || filename.equals(""))
                {
                    //do nothing 
                }
                else
                {
                    File uploadFile = new File(uploadDir + File.separator + randomFileName);
                    item.write(uploadFile);

                                            data.addFile(item.getFieldname(), item.getString());
                }
            }
        }
    }
    catch(FileSizeLimitExceededException e)
    {
        throw e;
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


    return data;
}

After parsing the request I am storing it in some object called MultipartFormData which can be used to get request parameters解析请求后,我将其存储在一个名为 MultipartFormData 的对象中,该对象可用于获取请求参数

public class MultiPartFormData {

private Hashtable<String, String> parameters;
    private Hashtable<String, String> uploadedFiles;

public MultiPartFormData()
{
    this.parameters = new Hashtable<String, String>();
    this.uploadedFiles = new Hashtable<String, String>();
}

public Hashtable<String, String> getParameters() {
    return parameters;
}
public void setParameters(Hashtable<String, String> parameters) {
    this.parameters = parameters;
}
    public void getParameter(String paramName) {
          if(this.parameters.contains(paramName))
                 return tyhis.parameters.get(paramName);
          return null;
    }
    public void addFile(String key, String filename) {
        uploadedFile.put(key, filename);
    }
    public void getFilename(String key) {
        uploadedFile.get(key);
    }
}

I had a similar problem, and in that case the reason was: The IT department had set some settings on their firewall that stopped the execution of some of my scripts and only for some of the clients.我遇到了类似的问题,在那种情况下,原因是:IT 部门在他们的防火墙上设置了一些设置,这些设置停止了我的一些脚本的执行,并且只针对一些客户端。 The halt sent the browser to the root web page of my server.暂停将浏览器发送到我服务器的根网页。 They were not able to explain in detail, but after opening up some filters, it all works as intended.他们无法详细解释,但打开一些过滤器后,一切都按预期工作。 WAMP, PHP7. WAMP,PHP7。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法处理部件,因为没有提供多部件配置 enctype='multipart/form-data' 使用问题 - Unable to process parts as no multi-part configuration has been provided enctype='multipart/form-data' usage issue h:form enctype =“ multipart / form-data”不执行任何操作 - h:form enctype=“multipart/form-data” not firing any action enctype =“ multipart / form-data”破坏了我的应用 - enctype=“multipart/form-data” destroyed my app 使用enctype =“ multipart / form-data”将选定的图像获取到控制器类吗? - Get selected image to Controller Class with enctype=“multipart/form-data”? 了解Spring MVC中的enctype = multipart / form-data的工作 - Understanding working of enctype = multipart/form-data in spring mvc 在 enctype=&quot;multipart/form-data&quot; 请求不起作用之后 - after enctype="multipart/form-data" request not working 当提交的表单具有属性 enctype=&quot;multipart/form-data&quot; 时,如何在控制器中获取表单数据? - How to get form data in controller when the form that was submitted has the attribute enctype="multipart/form-data"? 使用 enctype=multipart/form-data 上传文件时出错,错误是上传的文件不是 multipart - error while file uploading using enctype=multipart/form-data , the error is the file bring uploaded is not multipart 尽管我写了“enctype=&quot;multipart/form-data”,但发生错误“当前请求不是多部分请求” - Although I wrote 'enctype="multipart/form-data', ERROR 'Current request is not a multipart request' happen 使用enctype =“multipart / form-data”的表单会导致访问隐藏字段的问题 - Does form with enctype=“multipart/form-data” cause problems accessing a hidden field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM