使用webkitDirectory和Java Servlet上載文件夾/子目錄中的文件

[英]Upload files in folder/subdirectories using webkitDirectory and Java Servlet


I am trying to implement a file-upload functionality using webkitdirectory with java backend.

我正在嘗試使用帶有java后端的webkitdirectory實現文件上載功能。

Step1. Design a HTML form with webkitdirectory

步驟1。使用webkitdirectory設計HTML表單

<form action="DataUpload" method="post" enctype="multipart/form-data">
<input type="text" name="dbName" value="Database Name Here" id="dbName"/>
    <input type="file" id="ctrl" webkitdirectory directory multiple/>
    <input type="submit" />
</form>

Step 2. Passing information from form to Servlet

第2步。將信息從表單傳遞到Servlet

public class DataUpload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response){

    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator fileIterator;
    try {
        fileIterator = upload.getItemIterator(request);
        InputStream inputStream = null;
        BufferedReader br = null;
        System.out.println("CheckPoint 1");
        while(fileIterator.hasNext()) {
            System.out.println("CheckPoint 2");
            FileItemStream item = fileIterator.next();
            String inputFileName = FilenameUtils.getName(item.getName());
            inputStream = item.openStream();

            inputFileName = inputFileName.split("\\.")[0];
            List<String[]> list = new ArrayList<String[]>();                
            // Getting File
            br = new BufferedReader(new InputStreamReader(inputStream));    // Getting the object to read file
            String line;
            while((line = br.readLine())!= null){// While condition ends then end of file is reached.
                list.add(line.split(","));
            }
            // Checking if File is Empty
            if (list.size() == 0){
                System.err.println("File Empty");
            }else{
                // TODO : Parameter Parser.
                // DO JOB HERE
            }           
        }
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

My code, doesn't give any programming error, but it does not pass through the CheckPoint 2, i.e. it doesn't go inside the while loop. I tried looking into various post, such as:

我的代碼,不會給出任何編程錯誤,但它不會通過CheckPoint 2,即它不會進入while循環。我試着查看各種帖子,例如:

Keep Directory Structure When Uploading
How to upload files to server using JSP/Servlet? - While this question shows maximum resemblance to problem in question, This question works for selecting multiple files in a folder, where the problem here is question is to upload files in different sub directories inside a folder.

上傳時保留目錄結構如何使用JSP / Servlet將文件上傳到服務器? - 雖然這個問題與所討論的問題最相似,但這個問題適用於選擇文件夾中的多個文件,這里的問題是將文件上傳到文件夾內的不同子目錄中。

I was wondering, if this was possible using solely java servlets without using javascript. I was able to upload multiple files inside a single folder. But, code doesn't seem to work, when I select a folder as input, instead it works when I select a particular file or a subset of files.

我想知道,如果這可能只使用java servlet而不使用javascript。我能夠在單個文件夾中上傳多個文件。但是,當我選擇一個文件夾作為輸入時,代碼似乎不起作用,而是在我選擇特定文件或文件子集時它可以工作。

2 个解决方案

#1


0  

HTML File Form

HTML文件表單

<form onsubmit="readFiles()">
    <input type="file" name="files" webkitdirectory directory multiple id="files">
    <input type="submit">
</form>

JavaScript Function

function readFiles(){
    var files = document.getElementById("files").files;
    for (var file = 0 ; file < files.length; file++) {
        var reader = new FileReader();
        reader.onload = function(e){
            var object = new Object();
            object.content = e.target.content;
            var json_upload = "jsonObject=" + JSON.stringify(object);
            var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
            xmlhttp.open("POST", "http://localhost:8080/readFileServlet");
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send(json_upload);
        }
        reader.readAsBinaryString(files);
    }
}

Java Function:

JSONObject jObj = new JSONObject(request.getParameter("jsonObject"));

#2


-1  

in a post method try

在post方法中試試

make @Autowired ServletContext c; or take object from it in servlets

make @Autowired ServletContext c;或者在servlet中從中獲取對象

        byte[] bytes = file.getBytes();

         String UPLOAD_FOLDEdR=c.getRealPath("/images");     
        Path path = Paths.get(UPLOAD_FOLDEdR +"/"+ file.getOriginalFilename());
        Files.write(path, bytes);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/23/7300d3705b9616b06d2d77377c661d00.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com