Example usage for javax.servlet.http Part getHeader

List of usage examples for javax.servlet.http Part getHeader

Introduction

In this page you can find the example usage for javax.servlet.http Part getHeader.

Prototype

public String getHeader(String name);

Source Link

Document

Returns the value of the specified mime header as a String.

Usage

From source file:com.ahm.fileupload.FileController.java

private String getFileName(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1);
        }//from   ww  w  .  j  ava  2 s.  c o  m
    }
    return null;
}

From source file:io.stallion.contentPublishing.UploadRequestProcessor.java

private String getFileNameFromPart(final Part part) {
    final String partHeader = part.getHeader("content-disposition");
    Log.info("Part Header = {0}", partHeader);
    for (String content : part.getHeader("content-disposition").split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
        }/* w  w w. ja  va  2  s  .co m*/
    }
    return null;
}

From source file:com.sishuok.chapter4.web.servlet.UploadServlet.java

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {

    try {/* www  .  ja v a  2s .co  m*/

        //servlet?Partnull
        //9.0.4.v20130625 ??
        System.out.println(req.getParameter("name"));

        //?Part
        System.out.println("\n\n==========file1");
        Part file1Part = req.getPart("file1"); //?? ?Part
        InputStream file1PartInputStream = file1Part.getInputStream();
        System.out.println(IOUtils.toString(file1PartInputStream));
        file1PartInputStream.close();

        // ??
        System.out.println("\n\n==========file2");
        Part file2Part = req.getPart("file2");
        InputStream file2PartInputStream = file2Part.getInputStream();
        System.out.println(IOUtils.toString(file2PartInputStream));
        file2PartInputStream.close();

        System.out.println("\n\n==========parameter name");
        //????
        System.out.println(IOUtils.toString(req.getPart("name").getInputStream()));
        //Part??? jettyparameters??
        System.out.println(req.getParameter("name"));

        //?? ? req.getInputStream(); ??

        System.out.println("\n\n=============all part");
        for (Part part : req.getParts()) {
            System.out.println("\n\n=========name:::" + part.getName());
            System.out.println("=========size:::" + part.getSize());
            System.out.println("=========content-type:::" + part.getContentType());
            System.out
                    .println("=========header content-disposition:::" + part.getHeader("content-disposition"));
            System.out.println("=========file name:::" + getFileName(part));
            InputStream partInputStream = part.getInputStream();
            System.out.println("=========value:::" + IOUtils.toString(partInputStream));

            //
            partInputStream.close();
            // ? ? ?
            part.delete();

        }

    } catch (IllegalStateException ise) {
        //
        ise.printStackTrace();
        String errorMsg = ise.getMessage();
        if (errorMsg.contains("Request exceeds maxRequestSize")) {
            //?
        } else if (errorMsg.contains("Multipart Mime part file1 exceeds max filesize")) {
            //? ??
        } else {
            //
        }
    }

}

From source file:edu.lternet.pasta.portal.HarvesterServlet.java

private String getFilename(Part part) {
    String contentDispositionHeader = part.getHeader("content-disposition");
    String[] elements = contentDispositionHeader.split(";");
    for (String element : elements) {
        if (element.trim().startsWith("filename")) {
            return element.substring(element.indexOf('=') + 1).trim().replace("\"", "");
        }//from   www  .  j ava2 s .  c  om
    }
    return null;
}

From source file:ips1ap101.lib.core.jsf.JSF.java

private static String getPartFileName(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
        }//from   w ww.  j  av  a 2 s  . co m
    }
    return null;
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImplTest.java

private Part createPart(String name, InputStream inputStream, String contentDisposition) throws IOException {
    Part part = mock(Part.class);
    when(part.getName()).thenReturn(name);
    when(part.getInputStream()).thenReturn(inputStream);
    when(part.getHeader("Content-Disposition")).thenReturn(contentDisposition);
    when(part.getContentType()).thenReturn(MediaType.APPLICATION_OCTET_STREAM);
    return part;//  ww  w .  ja v a2s. com
}

From source file:org.codice.ddf.rest.impl.CatalogServiceImpl.java

@Override
public Map.Entry<AttachmentInfo, Metacard> parseParts(Collection<Part> contentParts, String transformerParam) {
    if (contentParts.size() == 1) {
        Part part = Iterables.get(contentParts, 0);

        try (InputStream inputStream = part.getInputStream()) {
            ContentDisposition contentDisposition = new ContentDisposition(
                    part.getHeader(HEADER_CONTENT_DISPOSITION));
            return new ImmutablePair<>(
                    attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(),
                            contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME)),
                    null);//from w  ww .jav  a 2s  .  c  om

        } catch (IOException e) {
            LOGGER.debug("IOException reading stream from file attachment in multipart body.", e);
        }
    }

    Metacard metacard = null;
    AttachmentInfo attachmentInfo = null;
    Map<String, AttributeImpl> attributeMap = new HashMap<>();

    for (Part part : contentParts) {
        String name = part.getName();
        String parsedName = (name.startsWith("parse.")) ? name.substring(6) : name;

        try (InputStream inputStream = part.getInputStream()) {
            ContentDisposition contentDisposition = new ContentDisposition(
                    part.getHeader(HEADER_CONTENT_DISPOSITION));
            switch (name) {
            case "parse.resource":
                attachmentInfo = attachmentParser.generateAttachmentInfo(inputStream, part.getContentType(),
                        contentDisposition.getParameter(FILENAME_CONTENT_DISPOSITION_PARAMETER_NAME));
                break;
            case "parse.metadata":
                metacard = parseMetacard(transformerParam, metacard, part, inputStream);
                break;
            default:
                parseOverrideAttributes(attributeMap, parsedName, inputStream);
                break;
            }
        } catch (IOException e) {
            LOGGER.debug("Unable to get input stream for mime attachment. Ignoring override attribute: {}",
                    name, e);
        }
    }
    if (attachmentInfo == null) {
        throw new IllegalArgumentException("No parse.resource specified in request.");
    }
    if (metacard == null) {
        metacard = new MetacardImpl();
    }

    Set<AttributeDescriptor> missingDescriptors = new HashSet<>();
    for (Attribute attribute : attributeMap.values()) {
        if (metacard.getMetacardType().getAttributeDescriptor(attribute.getName()) == null) {
            attributeRegistry.lookup(attribute.getName()).ifPresent(missingDescriptors::add);
        }
        metacard.setAttribute(attribute);
    }

    if (!missingDescriptors.isEmpty()) {
        MetacardType original = metacard.getMetacardType();
        MetacardImpl newMetacard = new MetacardImpl(metacard);
        newMetacard.setType(new MetacardTypeImpl(original.getName(), original, missingDescriptors));
        metacard = newMetacard;
    }

    return new ImmutablePair<>(attachmentInfo, metacard);
}

From source file:hu.api.SivaPlayerVideoServlet.java

/**
 * Extracts the filename from the {@link Part} header.
 * /*  ww  w  .  j a  va 2 s .  c om*/
 * @param part
 *            to parse
 * @return the filename
 */
private String getFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] tokens = contentDisp.split(";");
    for (String token : tokens) {
        if (token.trim().startsWith("filename")) {
            return token.substring(token.indexOf("=") + 2, token.length() - 1);
        }
    }
    return "";
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

private static String getSubmittedFileName(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String fileName = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1); // MSIE fix.
        }//from   w w w  . java 2 s.c  o m
    }
    return null;
}