2012-05-18 50 views
0

我想在我的程序中獲取下拉值,以便將字符串合併到我的文件路徑,以便根據用戶輸入動態地改變路徑.iam新到apache commoms,在我之前正在使用o'reilly api。在servlet中使用apache commons獲取下拉值

這裏是我的代碼:

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

我試圖調試和我收到的賴特文件路徑,但同時進一步出發,fileItems顯示大小= 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

request.getParameter()電話應該被刪除。它們導致在Apache Commons FileUpload解析它之前解析請求主體。 multipart/form-data請求不應使用request.getParameter()

你需要收集您的if (!fi.isFormField())else正常表單字段。

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

又見FileUpload User Guidethis answer

+0

:在烏拉圭回合的答案.getfieldname和.getname將處理輸入type.the同樣會爲下拉工作? – ksa

+0

我發佈的鏈接背後有詳細的例子。 – BalusC

+0

謝謝u.i會盡我所能。 – ksa

相關問題