JSP can upload files to the server through HTML forms. The file type can be a text file, a binary file, an image file, or any other document.
Next we use HTML tags to create the file upload form. The following points should be noted:
The form method attribute must be set to the POST method, and the GET method cannot be used.
The form enctype attribute needs to be set to multipart/form-data.
The form action attribute needs to be set to the jsp file address submitted to the background for file upload. For example, the uploadFile.jsp program file is used to process uploaded files.
To upload a file element, you need to use the <input .../> tag and set the attribute to type="file". If you need to upload multiple files, you can set different names in the <input .../> tag.
The following is a form for uploading files. An example is as follows:
<html><head><title>File Uploading Form</title></head><body><h3>File Upload:</h3>Select a file to upload: <br /><form action="UploadServlet" method="post" enctype="multipart/form-data"><input type="file" name="file" size="50" /><br /><input type="submit" value="Upload File " /></form></body></html>Access the file in your local browser. The display interface is as follows. When you click "Upload File", a window will pop up allowing you to select the file to upload:
First, we first define the location where the file is stored on the service after uploading. You can write the path in your program, or we can set the directory where the file is stored by setting the context-param element in the web.xml configuration file, as follows Show:
<web-app>....<context-param> <description>File upload address</description> <param-name>file-upload</param-name> <param-value> c:apache-tomcat- 5.5.29webappsdata </param-value> </context-param>....</web-app>
The following script file UploadFile.jsp can handle multiple uploaded files. Before using this script, we need to pay attention to the following points:
The following example relies on FileUpload, so you need to introduce the latest commons-fileupload.xxjar package file into your classpath. The download address is: http://commons.apache.org/fileupload/.
FileUpload relies on Commons IO, so you need to introduce the latest commons-io-xxjar into your classpath. The download address is: http://commons.apache.org/io/.
When testing the following example, you need to upload and confirm that the size of the uploaded file is smaller than the size set by the maxFileSize variable, otherwise the file cannot be uploaded successfully.
Make sure you have created the directories c:temp and c:apache-tomcat-5.5.29webappsdata.
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %><%@ page import="javax.servlet.http.*" %><%@ page import= "org.apache.commons.fileupload.*" %><%@ page import="org.apache.commons.fileupload.disk.*" %><%@ page import="org.apache.commons.fileupload.servlet.*" %><%@ page import="org.apache.commons.io.output.*" %><% File file; int maxFileSize = 5000 * 1024; int maxMemSize = 5000 * 1024; ServletContext context = pageContext.getServletContext(); String filePath = context.getInitParameter("file-upload"); // Verify the type of uploaded content String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // Set the maximum value of files stored in memory factory.setSizeThreshold(maxMemSize); // Locally stored data is greater than maxMemSize. factory.setRepository(new File("c:\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set the maximum uploaded file Size upload.setSizeMax( maxFileSize ); try{ // Parse the obtained file List fileItems = upload.parseRequest(request); // Process uploaded files Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>JSP File upload</title> "); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // Get the parameters of the uploaded file String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory (); long sizeInBytes = fi.getSize(); // Write to file if( fileName.lastIndexOf("\") >= 0 ){ file = new File( filePath , fileName.substring( fileName.lastIndexOf("\"))) ; }else{ file = new File( filePath , fileName.substring(fileName.lastIndexOf("\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } }else{ out.println("< html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html> "); }%>
Next, let us access http://localhost:8080/UploadFile.htm through the browser. The interface is as shown below, and upload the file:
If your JSP script runs normally, the file will be uploaded to c:apache-tomcat-5.5.29webappsdata. You can open the folder to see if the upload is successful.