使用servlet中的apache commons获取下拉值

时间:2012-05-18 05:29:06

标签: html servlets apache-commons

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

这是我的代码:

 @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>

1 个答案:

答案 0 :(得分:1)

应删除request.getParameter()次来电。它们导致在Apache Commons FileUpload可以解析它之前解析请求体。不应在request.getParameter()请求中使用multipart/form-data

您需要在else的{​​{1}}中收集普通表单字段。

if (!fi.isFormField())

另请参阅FileUpload User Guidethis answer