【问题标题】:getting dropdown values using apache commons in servlet在 servlet 中使用 apache commons 获取下拉值
【发布时间】:2012-05-18 19:44:17
【问题描述】:

我想在我的程序中获取下拉值,以便将字符串合并到我的文件路径中,以便路径将根据用户输入动态更改。在我使用 o'reilly api 之前,我是 apache commoms 的新手。

这是我的代码:

 @Override
public void doPost(HttpServletRequest request, 
           HttpServletResponse response)
          throws ServletException, java.io.IOException 
{
       //FileItem f1;
   String d1= request.getParameter("sel1");
    String d2=request.getParameter("sel2");
    String d3="/home/adapco/Desktop/output";
    String conc=d3+"/"+d1+"/"+d2+"/";
    filePath=(new StringBuilder()).append(conc).toString();
 //      filePath="/home/adapco/Desktop/output/";
  isMultipart = ServletFileUpload.isMultipartContent(request);
}

我尝试调试,我得到了 wright 文件路径,但在继续进行时,fileItems 显示 size=0,并且由于 size0,它没有进入循环。

    filePath="/home/adapco/Desktop/output/";

如果我将上传路径传递给 filePath 它工作正常。

  List fileItems = upload.parseRequest(request);    
  Iterator i = fileItems.iterator();
  while ( i.hasNext () ) 
  {
     FileItem fi = (FileItem)i.next();
     if ( !fi.isFormField () )  
     {         
        String fieldName = fi.getFieldName();
        String fileName = fi.getName();
        String contentType = fi.getContentType();
        boolean isInMemory = fi.isInMemory();
        long sizeInBytes = fi.getSize();
        if( fileName.lastIndexOf("\\") >= 0 ){
           file = new File( filePath + 
           fileName.substring( fileName.lastIndexOf("\\"))) ;
        }else{
           file = new File( filePath + 
           fileName.substring(fileName.lastIndexOf("\\")+1)) ;
        }
        fi.write( file ) ;
        out.println("Uploaded Filename: " + fileName + "<br>"+filePath);

     }

我的html:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
 <title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
 Select a file to upload: <br />
 <form action="upload" method="post"
                    enctype="multipart/form-data">
<input type="file" name="file">
 <br />
<select name="sel1"> 
<option label ="1">aerospace</option>
<option label ="2">automotive</option>
</select>
<select name="sel2">
<option label="1">internal</option>
<option label="2">demo</option>
 </select>
 <input type="submit" value="Upload File" />
 </form>
 </body>
</html>

【问题讨论】:

    标签: html servlets apache-commons


    【解决方案1】:

    应该删除request.getParameter() 调用。它们导致请求正文在 Apache Commons FileUpload 解析之前被解析。 request.getParameter() 不应用于 multipart/form-data 请求。

    您需要在if (!fi.isFormField())else 中收集普通表单字段。

    if (!fi.isFormField()) {
        // Collect uploaded files.
    }
    else {
        // Collect normal form fields.
    }
    

    另请参阅 FileUpload User Guidethis answer

    【讨论】:

    • :在你的回答中,.getfieldname 和 .getname 将处理输入类型。同样适用于下拉菜单?
    • 我贴的链接后面有详细的例子。
    • 没问题。这并不难,只需将鼠标指针移至蓝色的“this answer”文本,然后用力按下左键即可。这比您输入评论所需的工作量要少。
    • @baluc 我试过了,终于解决了(更改了代码),它运行良好。
    • @baluc:我已经开始了一个新线程,我已经在这个链接中发布了我部分运行的程序:stackoverflow.com/questions/10697317/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2013-01-01
    • 2013-06-15
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多