Skip to content

Commit

Permalink
write files and support having a top-level directory for uploaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
gashcrumb committed Jun 21, 2013
1 parent 6913dc4 commit 196923b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
60 changes: 57 additions & 3 deletions hawtio-web/src/main/java/io/hawt/web/UploadServlet.java
Expand Up @@ -16,6 +16,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -25,23 +26,43 @@ public class UploadServlet extends HttpServlet {

private static final transient Logger LOG = LoggerFactory.getLogger(UploadServlet.class);

// TODO - make more configurable
public static String UPLOAD_DIRECTORY = System.getProperty("java.io.tmpdir") + File.separator + "uploads";

static {
LOG.info("Using file upload directory: {}", UPLOAD_DIRECTORY);
}

private static DiskFileItemFactory newDiskFileItemFactory(ServletContext context, File repository) {
FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);
DiskFileItemFactory factory = new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);
factory.setFileCleaningTracker(fileCleaningTracker);
return factory;
}

private static String sanitize(String name) {
return name.replaceAll("[^0-9a-zA-Z\\+\\.\\(\\)_\\-]","");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
ServletContext context = this.getServletConfig().getServletContext();
File repository = (File) context.getAttribute("javax.servlet.context.tempdir");
DiskFileItemFactory factory = newDiskFileItemFactory(context, repository);
File uploadDir = new File(UPLOAD_DIRECTORY);
if (!uploadDir.exists()) {
LOG.info("Creating directory {}" + uploadDir);
if (!uploadDir.mkdirs()) {
LOG.warn("Failed to create upload directory at {}", uploadDir);
}
}
DiskFileItemFactory factory = newDiskFileItemFactory(context, uploadDir);
ServletFileUpload upload = new ServletFileUpload(factory);

String targetDirectory = null;
List<File> files = new ArrayList<File>();

try {
List<FileItem> items = upload.parseRequest(request);

Expand All @@ -50,20 +71,53 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String name = item.getFieldName();
String value = item.getString();
LOG.info("Got form field {} with value {}", name, value);
if (name.equals("parent")) {
targetDirectory = value;
}
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();

fileName = sanitize(fileName);

LOG.info("Got file upload, fieldName: {} fileName: {} contentType: {} size: {}", new Object[]{fieldName, fileName, contentType, sizeInBytes});

File target = new File(UPLOAD_DIRECTORY + File.separator + fileName);

try {
item.write(target);
files.add(target);
LOG.info("Wrote to file: {}", target.getAbsoluteFile());
} catch (Exception e) {
LOG.warn("Failed to write to {} due to {}", target, e);
throw new RuntimeException(e);
}
}
}
} catch (FileUploadException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}

if (targetDirectory != null) {
targetDirectory = sanitize(targetDirectory).replace(".", "");
File target = new File(uploadDir.getAbsolutePath() + File.separator + targetDirectory);
LOG.info("Putting files in subdirectory: {}", targetDirectory);
if (!target.exists()) {
if (!target.mkdirs()) {
LOG.warn("Failed to create target directory: {}", target); }
}

for (File file : files) {
File dest = new File(target.getAbsolutePath() + File.separator + file.getName());
LOG.info("Renaming {} to {}", file, dest);
if (!file.renameTo(dest)) {
LOG.warn("Failed to rename {} to {}", file, dest);
}
}
}


} else {
super.doPost(request, response);
Expand Down
1 change: 1 addition & 0 deletions hawtio-web/src/main/webapp/app/ui/html/test.html
Expand Up @@ -6,6 +6,7 @@
<div class="row-fluid">
<form action="upload" method="post" enctype="multipart/form-data">
<fieldset>
<input type="text" name="parent" value="test">
<input type="file" name="test-file">
<input type="submit">
</fieldset>
Expand Down

0 comments on commit 196923b

Please sign in to comment.