How to Upload a Folder Into Java
Certificate Information
Preface
Part I Introduction
one. Overview
2. Using the Tutorial Examples
Function II The Web Tier
3. Getting Started with Web Applications
four. JavaServer Faces Engineering
five. Introduction to Facelets
6. Expression Language
7. Using JavaServer Faces Applied science in Spider web Pages
8. Using Converters, Listeners, and Validators
9. Developing with JavaServer Faces Applied science
10. JavaServer Faces Engineering: Advanced Concepts
11. Using Ajax with JavaServer Faces Applied science
12. Blended Components: Avant-garde Topics and Example
13. Creating Custom UI Components and Other Custom Objects
14. Configuring JavaServer Faces Applications
15. Java Servlet Technology
16. Uploading Files with Java Servlet Engineering science
The @MultipartConfig Annotation
The getParts and getPart Methods
17. Internationalizing and Localizing Spider web Applications
Office III Spider web Services
xviii. Introduction to Web Services
nineteen. Building Web Services with JAX-WS
20. Edifice RESTful Web Services with JAX-RS
21. JAX-RS: Avant-garde Topics and Example
Part Iv Enterprise Beans
22. Enterprise Beans
23. Getting Started with Enterprise Beans
24. Running the Enterprise Bean Examples
25. A Message-Driven Bean Example
26. Using the Embedded Enterprise Edible bean Container
27. Using Asynchronous Method Invocation in Session Beans
Part V Contexts and Dependency Injection for the Java EE Platform
28. Introduction to Contexts and Dependency Injection for the Java EE Platform
29. Running the Basic Contexts and Dependency Injection Examples
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Avant-garde Contexts and Dependency Injection Examples
Function Vi Persistence
32. Introduction to the Java Persistence API
33. Running the Persistence Examples
34. The Java Persistence Query Language
35. Using the Criteria API to Create Queries
36. Creating and Using String-Based Criteria Queries
37. Controlling Concurrent Admission to Entity Data with Locking
38. Using a 2d-Level Cache with Java Persistence API Applications
Part VII Security
39. Introduction to Security in the Coffee EE Platform
40. Getting Started Securing Web Applications
41. Getting Started Securing Enterprise Applications
42. Java EE Security: Advanced Topics
Function Viii Java EE Supporting Technologies
43. Introduction to Java EE Supporting Technologies
44. Transactions
45. Resources and Resources Adapters
46. The Resource Adapter Example
47. Java Message Service Concepts
48. Java Message Service Examples
49. Edible bean Validation: Advanced Topics
50. Using Java EE Interceptors
Part Ix Case Studies
51. Duke's Bookstore Case Study Example
52. Duke's Tutoring Example Study Example
53. Duke's Forest Case Study Instance
Index
The fileupload Case Application
The fileupload case illustrates how to implement and use the file upload feature.
The Duke'southward Woods case written report provides a more than complex example that uploads an image file and stores its content in a database.
Architecture of the fileupload Case Application
The fileupload example application consists of a single servlet and an HTML form that makes a file upload request to the servlet.
This example includes a very uncomplicated HTML form with two fields, File and Destination. The input type, file, enables a user to browse the local file organisation to select the file. When the file is selected, it is sent to the server as a part of a Mail request. During this process ii mandatory restrictions are applied to the class with input type file:
-
The enctype attribute must be set to a value of multipart/form-information.
-
Its method must be Mail.
When the grade is specified in this manner, the entire asking is sent to the server in encoded grade. The servlet and then handles the request to process the incoming file information and to extract a file from the stream. The destination is the path to the location where the file will be saved on your calculator. Pressing the Upload button at the bottom of the form posts the data to the servlet, which saves the file in the specified destination.
The HTML course in tut-install /examples/web/fileupload/web/index.html is every bit follows:
<!DOCTYPE html> <html lang="en"> <head> <championship>File Upload</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form method="POST" action="upload" enctype="multipart/form-data" > File: <input type="file" name="file" id="file" /> <br/> Destination: <input type="text" value="/tmp" name="destination"/> </br> <input type="submit" value="Upload" proper name="upload" id="upload" /> </form> </body> </html>
A POST request method is used when the customer needs to transport information to the server every bit part of the request, such as when uploading a file or submitting a completed grade. In contrast, a GET request method sends a URL and headers merely to the server, whereas Mail service requests besides include a message torso. This allows arbitrary-length data of any blazon to be sent to the server. A header field in the POST asking usually indicates the bulletin torso'south Internet media type.
When submitting a course, the browser streams the content in, combining all parts, with each part representing a field of a grade. Parts are named later on the input elements and are separated from each other with cord delimiters named boundary.
This is what submitted data from the fileupload grade looks like, after selecting sample.txt every bit the file that will be uploaded to the tmp directory on the local file system:
POST /fileupload/upload HTTP/ane.1 Host: localhost:8080 Content-Blazon: multipart/form-data; boundary=---------------------------263081694432439 Content-Length: 441 -----------------------------263081694432439 Content-Disposition: form-data; name="file"; filename="sample.txt" Content-Type: text/apparently Data from sample file -----------------------------263081694432439 Content-Disposition: form-information; name="destination" /tmp -----------------------------263081694432439 Content-Disposition: form-data; name="upload" Upload -----------------------------263081694432439--
The servlet FileUploadServlet.java can be found in the tut-install /examples/web/fileupload/src/java/fileupload/ directory. The servlet begins as follows:
@WebServlet(proper name = "FileUploadServlet", urlPatterns = {"/upload"}) @MultipartConfig public class FileUploadServlet extends HttpServlet { individual last static Logger LOGGER = Logger.getLogger(FileUploadServlet.class.getCanonicalName());
The @WebServlet annotation uses the urlPatterns property to ascertain servlet mappings.
The @MultipartConfig annotation indicates that the servlet expects requests to made using the multipart/grade-data MIME blazon.
The processRequest method retrieves the destination and file part from the request, so calls the getFileName method to retrieve the file proper noun from the file office. The method and so creates a FileOutputStream and copies the file to the specified destination. The error-handling section of the method catches and handles some of the most mutual reasons why a file would not be found. The processRequest and getFileName methods look like this:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); // Create path components to save the file final String path = asking.getParameter("destination"); final Office filePart = asking.getPart("file"); final String fileName = getFileName(filePart); OutputStream out = nothing; InputStream filecontent = null; final PrintWriter writer = response.getWriter(); endeavour { out = new FileOutputStream(new File(path + File.separator + fileName)); filecontent = filePart.getInputStream(); int read = 0; concluding byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes)) != -ane) { out.write(bytes, 0, read); } writer.println("New file " + fileName + " created at " + path); LOGGER.log(Level.INFO, "File{0}being uploaded to {1}", new Object[]{fileName, path}); } take hold of (FileNotFoundException fne) { writer.println("Yous either did not specify a file to upload or are " + "trying to upload a file to a protected or nonexistent " + "location."); writer.println("<br/> Mistake: " + fne.getMessage()); LOGGER.log(Level.SEVERE, "Issues during file upload. Error: {0}", new Object[]{fne.getMessage()}); } finally { if (out != null) { out.close(); } if (filecontent != aught) { filecontent.close(); } if (writer != null) { writer.close(); } } } private String getFileName(last Part role) { final String partHeader = function.getHeader("content-disposition"); LOGGER.log(Level.INFO, "Part Header = {0}", partHeader); for (String content : part.getHeader("content-disposition").dissever(";")) { if (content.trim().startsWith("filename")) { render content.substring( content.indexOf('=') + 1).trim().supersede("\"", ""); } } return goose egg; }
Running the fileupload Case
You lot tin can use either NetBeans IDE or Pismire to build, package, deploy, and run the fileupload case.
To Build, Package, and Deploy the fileupload Example Using NetBeans IDE
- From the File bill of fare, cull Open up Project.
- In the Open up Project dialog, navigate to:
tut-install /examples/spider web/
- Select the fileupload folder.
- Select the Open as Main Project checkbox.
- Click Open up Projection.
- In the Projects tab, correct-click fileupload and select Deploy.
To Build, Package, and Deploy the fileupload Case Using Pismire
- In a terminal window, go to:
tut-install /examples/web/fileupload/
- Type the following command:
ant
- Blazon the post-obit command:
pismire deploy
To Run the fileupload Example
- In a web browser, type the following URL:
http://localhost:8080/fileupload/
The File Upload page opens.
- Click Browse to display a file browser window.
- Select a file to upload and click Open.
The proper noun of the file you selected is displayed in the File field. If you do not select a file, an exception will be thrown.
- In the Destination field, type a directory name.
The directory must accept already been created and must also be writable. If you do not enter a directory name, or if you lot enter the name of a nonexistent or protected directory, an exception will be thrown.
- Click Upload to upload the file y'all selected to the directory y'all specified in the Destination field.
A message reports that the file was created in the directory you lot specified.
- Become to the directory you specified in the Destination field and verify that the uploaded file is present.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices
cowardhosturnelf1978.blogspot.com
Source: https://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
0 Response to "How to Upload a Folder Into Java"
Post a Comment