Upload handling
Your application may have forms with which visitors can upload images. For example, the State Department has an Upload Photo page for submitting a photograph when applying for passports.
In HTML development, uploading a file requires a form on the client with an attribute enctype="multipart/form-data", and the form itself contains an element <input type="file">.
In the following example, you implement a front-end form and back-end class that uploads an image and saves it using the default storage configuration defined in context.xml. The upload method from the browser is post.
Step 1: Implement HTML form
Implement an HTML form in a template.
HTML form for uploading a file:
1<h2>Upload File</h2>23<form enctype="multipart/form-data" method="post" action="/upload-servlet">4<p><input type="file" name="file" /></p>5<p><input type="text" name="description" /></p>6<p><input type="submit" /></p>7</form>
- 3. Provides the upload method (post) and specifies the back-end servlet processing the uploaded form (upload-servlet).
- 4. Provides the name attribute for the file in the uploaded form.
You’ll use both of these attributes when implementing the upload servlet.
Step 2: Implement upload servlet
Implement a servlet that accepts the uploaded form and saves the image to storage.
1import com.psddev.dari.util.ObjectUtils;2import com.psddev.dari.util.StorageItem;3import com.psddev.dari.util.StorageItemFilter;45import javax.servlet.ServletException;6import javax.servlet.annotation.WebServlet;7import javax.servlet.http.HttpServlet;8import javax.servlet.http.HttpServletRequest;9import javax.servlet.http.HttpServletResponse;10import java.io.IOException;11import java.io.PrintWriter;1213@WebServlet("/upload-servlet")14public class UploadServlet extends HttpServlet {1516@Override17protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {18StorageItem storageItem = StorageItemFilter.getParameter(request,"file", null);1920Map<String, Object> properties = new HashMap<String, Object>();21properties.put("path", storageItem.getPath());22properties.put("storageName", storageItem.getStorage());2324response.setContentType("application/json");25PrintWriter out = response.getWriter();26out.print(ObjectUtils.toJson(properties));27out.flush();28}29}
- 13. Associates the servlet with the action specified in "HTML form for uploading a file."
- 16. Overrides the
doPostmethod. We use this method because the upload form in "HTML form for uploading a file" specifiesmethod=post. - 18. Extracts the file from the form data using the value specified in the upload form name=file. The last parameter,
null, indicates Brightspot uses the default storage item configuration appearing in the Tomcatcontext.xmlfile. For information about configuring storage items, see Configuring StorageItem. - 20. Builds a map of key-value pairs describing the file’s storage characteristics. For additional methods available for examining the uploaded file, see Interface StorageItem.
- 24. Builds a JSON object from the map and returns it to the client.
1{2"path": "33/c7/f340bec94c21b7e88687c4f78fa4/dragon-slayer.jpg",3"storageName": "tutorial.local"4}
StorageItemFilter provides useful methods for extracting uploaded files from an uploaded form. For details, see Class StorageItemFilter.
See also: