Как создать простую загрузку файлов?

I've recently built a small web-site on top of Jease where people should be able to upload some MP3-files for a party. Allowing people to access the JeaseCMS-interface was no option (although Jease is easy to use, this is overkill for such a fire-and-forget-task), so I had to build a small JSP-script which handles the upload directly via the public site.

The script makes use of Apache Commons Fileupload-Library which is distributed with Jease anyway, so all I had to do is to wire things together.

The upload-script is splitted in three parts:

  • The HTML-Code for the multipart-data upload-form. It provides an additional field for an (optional) title which serves as an example on how to get attributes from a multipart-data-form.
  • The Java-Code for parsing the request and transfering the contents from the request into a Jease-File-Object.
  • A simple JSP-Snippet which iterates all files in the upload-folder to display a simple directory listing of submitted files.

To use the script below, you can simply create a "Script" within the JeaseCMS and copy&paste the content of the script below.

Have fun!

<%@page import="org.apache.commons.io.*" %>
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.servlet.*" %>
<%@page import="jfix.util.*" %>
<%@page import="jease.cmf.service.*" %>
<%@page import="jease.cms.domain.*" %>

<%-- Display file upload form --%>

<form action="" method="post" enctype="multipart/form-data">
<table>
  <tr>
    <td>Title:</td>
    <td><input type="text" name="title" size="40" /></td>
  </tr>
  <tr>
    <td>File:</td>
    <td><input name="file" type="file" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="submit" type="submit" value="Submit" /></td>
  </tr>
</table>
</form>

<%-- Upload submitted form into file object into uploadFolder --%> 

<%
  Folder uploadFolder = (Folder) ((Content) request.getAttribute("Node")).getParent();
  if (ServletFileUpload.isMultipartContent(request)) {
    File cmsFile = new File();
    FileItemIterator iter = new ServletFileUpload().getItemIterator(request);
    while (iter.hasNext()) {
      FileItemStream item = iter.next();
      java.io.InputStream inputStream = item.openStream();
      if (item.isFormField()) {
        if (item.getFieldName().equals("title")) {
          cmsFile.setTitle(IOUtils.toString(inputStream, "UTF-8"));
        }
      } else {
        if (Validations.isNotEmpty(item.getName())) {
          if (Validations.isEmpty(cmsFile.getTitle())) {
            cmsFile.setTitle(item.getName());
          }
          cmsFile.setId(Filenames.asId(item.getName()));
          cmsFile.setContentType(item.getContentType());
          cmsFile.setLastModified(new java.util.Date());
          java.io.OutputStream outputStream = new java.io.FileOutputStream(cmsFile.getFile());
          IOUtils.copy(inputStream, outputStream);
          outputStream.close();
        }
      }
      inputStream.close();
    }
    try {
      Nodes.append(uploadFolder, cmsFile);
    } catch(Exception e) {
      out.println(e.getMessage());
    }
  }
%>

<%-- Create a simple directory listing for all files in uploadFolder --%>

<ul>
  <%
    for (File file : uploadFolder.getChildren(File.class)) {
  %>
  <li><a href="<%=request.getContextPath()%><%=file.getPath()%>"><%=file.getTitle()%></a></li>
  <%
    }
  %>
</ul>

Last modified on 2012-04-23 by Maik Jablonski

Java with Ease

1970-01-01

Java is a very productive development environment if you choose and combine the right tools and patterns.

Jease is the result of my personal quest bringing tools and patterns together which make the life of a web-developer as simple as it can be.