2016-03-07 38 views
0

這裏選擇一個數據輸入文件是完整的問題,我問過一個小一點在我剛纔的問題Get a path value in java file from html/jsp,在這裏,我主要有輸入文件作爲一個Excel工作表,此表作爲源數據庫,並且這個文件每天都會變化,因爲會有一個新的Excel表單被髮送。現在我直接使用下面的代碼。的使用文件選擇

但不是這個,我想盡量給我的索引頁文件選擇和一個按鈕(如下圖所示),當我選擇該文件,然後按下按鈕後,excelPath應設置爲那條路。

<input type="file" id="inputFile" name="inputFile"/> 
<input type="button" value="set"/> 

我不想上傳文件,只是路徑是不夠的。

在Java類中,我知道如何處理Excel數據,因爲我已經在Apache-poi工作。我需要知道的是我該如何通過這條道路。請讓我知道我該怎麼做。

謝謝

+0

文件選擇器顯示客戶端計算機的目錄。但是你想從服務器中選擇一條路徑。我可能在開發階段工作,但它不會在生產中工作。你確定? –

+0

嗨@Yusuf,是的。請讓我知道如何去做。 – user3872094

+0

你認爲:(你需要使用JavaScript和Java在一起是不容易。 –

回答

1

我希望下面的代碼適合你。首先你需要一個servlet來讀取服務器的文件結構;您可能需要編輯以下代碼才能根據需要進行修復;

import java.io.File; 
import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet(urlPatterns = { "/serverpath" }) 
public class ServerPath extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { 

     String link = "<li><a href=\"serverpath?path=%s\">%s</a>&nbsp;<input type=\"button\" value=\"select\" onclick=\"window.opener.document.getElementById('path').value = '%s'; window.close();\" /></li>"; 
     response.setContentType("text/html"); 
     try { 
      String path = req.getParameter("path"); 
      if (path == null) { 
       // take disk drives, for linux/is enough for me 
       response.getOutputStream().print(String.format(link, "/", "/", "/")); 
      } else { 
       File file = new File(path); //read clicked file path and its sub paths. 
       if (file.isDirectory()) { 
        File[] subDir = file.listFiles(); 
        for (File file2 : subDir) { 
         if (file2.isDirectory()) { 
          response.getOutputStream().println(
            String.format(link, file2.getAbsolutePath(), file2.getAbsolutePath(), file2.getAbsolutePath())); 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

html/jsp頁面的示例;

<input type="text" id="path" /> 
<input type="button" value="Select" onclick="window.open('serverpath', '', 'width=700,height=500,top=150,left=150,scrollbars=yes,location=no')"> 

enter image description here

+0

嗨優素福,感謝。我會盡力的。 – user3872094

+0

不客氣。 –