Example usage for org.apache.commons.fileupload.disk DiskFileItem getFieldName

List of usage examples for org.apache.commons.fileupload.disk DiskFileItem getFieldName

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.disk DiskFileItem getFieldName.

Prototype

public String getFieldName() 

Source Link

Document

Returns the name of the field in the multipart form corresponding to this file item.

Usage

From source file:v7db.files.buckets.BucketsServlet.java

private void doFormPost(HttpServletRequest request, HttpServletResponse response, BSONObject bucket)
        throws IOException {
    ObjectId uploadId = new ObjectId();

    BSONObject parameters = new BasicBSONObject();
    List<FileItem> files = new ArrayList<FileItem>();

    if (ServletFileUpload.isMultipartContent(request)) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {/*  w w  w. java2s .c o m*/
            for (Object _file : upload.parseRequest(request)) {
                FileItem file = (FileItem) _file;
                if (file.isFormField()) {
                    String v = file.getString();
                    parameters.put(file.getFieldName(), v);
                } else {
                    files.add(file);
                }
            }
        } catch (FileUploadException e) {
            throw new IOException(e);
        }

    } else {
        for (Entry<String, String[]> param : request.getParameterMap().entrySet()) {
            String[] v = param.getValue();
            if (v.length == 1)
                parameters.put(param.getKey(), v[0]);
            else
                parameters.put(param.getKey(), v);

        }
    }

    BSONObject result = new BasicBSONObject("_id", uploadId);
    BSONObject uploads = new BasicBSONObject();
    for (FileItem file : files) {
        DiskFileItem f = (DiskFileItem) file;
        // inline until 10KB
        if (f.isInMemory()) {
            uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.get(), uploadId,
                    f.getName(), f.getContentType()));
        } else {
            uploads.put(f.getFieldName(), storage.inlineOrInsertContentsAndBackRefs(10240, f.getStoreLocation(),
                    uploadId, f.getName(), f.getContentType()));
        }
        file.delete();
    }
    result.put("files", uploads);
    result.put("parameters", parameters);

    bucketCollection.update(new BasicDBObject("_id", bucket.get("_id")),
            new BasicDBObject("$push", new BasicDBObject("FormPost.data", result)));

    String redirect = BSONUtils.getString(bucket, "FormPost.redirect");
    // redirect mode
    if (StringUtils.isNotBlank(redirect)) {
        response.sendRedirect(redirect + "?v7_formpost_id=" + uploadId);
        return;
    }
    // echo mode

    // JSON does not work, see https://jira.mongodb.org/browse/JAVA-332
    // response.setContentType("application/json");
    // response.getWriter().write(JSON.serialize(result));
    byte[] bson = BSON.encode(result);
    response.getOutputStream().write(bson);
}