Reputation: 4532
I want to do the following in tomcat 5.5
1. upload a excel file
2. process the file based on some crieteria
3. show the result
I am able to do all from 2 to 3 but not able to upload a file in tomcat 5.5 and could not also find example.
Pleaes help me.
Upvotes: 2
Views: 369
Reputation: 1397
Use Apache’s Commons FileUpload and HttpClient.
Here some links to help you out.
Upvotes: 1
Reputation: 23903
Maybe you could give a try on Apache commons fileUpload
You can get an sample here
A more hands-on with not so much conceptual and clarification things could be found here.
On your Servlet you will just use something like:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 2
Reputation: 1983
Apache has provided an API for uploading a file. You can try this.
http://commons.apache.org/fileupload/using.html
Upvotes: 1