Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * REFERENCE:
- * http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824
- *
- */
- List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
- for (FileItem item : items) {
- if (item.isFormField()) {
- }else{ // Process the form file field (input type="file")
- String fileName = FilenameUtils.getName(item.getName());
- System.out.println("Filename -> " + fileName);
- String realPath = getServletContext().getRealPath("/");
- File file = new File(realPath+"/uploadFolder");
- file.mkdir();
- String prefix = FilenameUtils.getBaseName(fileName) + "_";
- String suffix = "." + FilenameUtils.getExtension(fileName);
- // Prepare filename prefix and suffix for an unique filename in upload folder.
- File tempFile = File.createTempFile(prefix, suffix,file);
- System.out.println("tempFile -> "+tempFile);
- item.write(tempFile); // File uploaded to "uploadFolder" in Web Server(Not database)
- // Save the File path(String) to Database now.
- }
- }
Add Comment
Please, Sign In to add comment