簡體   English   中英

如何使用文件夾位置讀取客戶端計算機上的文件

[英]how to read a file on client machine using the folder location

這是jsp文件上傳表單

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Uploading form</title>
</head>
<body>
    <form action="./read.do" method="post">
        Select the file to read: <input type="file" id="myFile" size="50">
        <br />
         path is: <input type="text" name="path" />

        <button type="button" onclick="myFunction()">Copy path</button>
        <input type="submit" value="Submit">
        <p id="demo"></p>

        <script>
function myFunction() {
    var x = document.getElementById("myFile").value;
     document.getElementsByTagName("INPUT")[3].setAttribute("value",x); 

}
</script>
    </form>
</body>
</html>

這里我沒有將文件上傳到我的服務器我只是采用文件的路徑並使用servlet中的以下代碼讀取文件

String path = req.getParameter("path");
            FileInputStream fis = null;
            File excel = new File(path);
            fis = new FileInputStream(excel);

當我在服務器存在的同一台機器上運行時,這工作正常。但是當我嘗試從其他機器訪問時,代碼正在我的機器中搜索路徑而不是在客戶端機器上如何過來這個。 我已經看到了一些其他的方式,如使用multipart,但文件正在加載到服務器,但為了我的要求,我不應該直接將文件上傳到我的服務器,我必須通過采取文件路徑在客戶端機器上讀取完整的文件。 請告訴我一些想法來實現這一目標

您可以嘗試JavaScript File Reader API,使用JS讀取客戶端的文件內容。 文件API

例如:

 var file = document.getElementById("fileForUpload").files[0]; if (file) { var reader = new FileReader(); reader.readAsText(file, "UTF-8"); reader.onload = function (evt) { document.getElementById("fileContents").innerHTML = evt.target.result; } reader.onerror = function (evt) { document.getElementById("fileContents").innerHTML = "error reading file"; } } 

或者您也可以使用HTML5的文件Reader HTML5 FileReader

文件上傳是一個多部分請求 request.getParameter()返回客戶端計算機上的文件路徑。 而不是那樣,您需要使用request.getPart()方法或apache-fileupload之類的庫來檢索遠程文件的輸入流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM