I am trying to implement a file-upload functionality using webkitdirectory with java backend.
Step1. Design a HTML form with webkitdirectory
<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
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:
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.
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.
HTML File Form
JavaScript Function
Java Function:
in a post method try
make
@Autowired ServletContext c
; or take object from it in servlets