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.curso.ejemplohinputfile.FileUploadBean.java

public static String getSubmittedFileName(Part filePart) {
    String header = filePart.getHeader("content-disposition");
    if (header == null) {
        return null;
    }// w w w  .j ava  2 s .  c  om
    for (String headerPart : header.split(";")) {
        if (headerPart.trim().startsWith("filename")) {
            return headerPart.substring(headerPart.indexOf('=') + 1).trim().replace("\"", "");
        }
    }
    return null;
}

From source file:com.mycompany.osgiwebfelix.MainServlet.java

private static 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); // MSIE fix.
        }//from ww w  . j  a  v a  2 s .  c o  m
    }
    return null;
}

From source file:gob.dp.simco.comun.FunctionUtil.java

private static 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  a  v a  2 s  .c  o m
    }
    return null;
}

From source file:easyproject.bean.CommentBean.java

public static 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 a  v  a2  s  .  c om*/
    }
    return null;
}

From source file:com.ibm.liberty.starter.api.v1.LibertyFileUploader.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("\"", "");
            log.log(Level.FINEST, "fileName=" + fileName + " : part=" + part);
            return fileName.substring(fileName.lastIndexOf('/') + 1).substring(fileName.lastIndexOf('\\') + 1);
        }/*from   w  w  w. j a va  2  s. c  o m*/
    }
    log.log(Level.FINE, "File name was not retrieved : part=" + part);
    return null;
}

From source file:org.nuxeo.ecm.platform.ui.web.util.files.FileUtils.java

/**
 * Helper method to retrieve filename from part, waiting for servlet-api related improvements.
 * <p>//  w w  w . j av a2s .  c o m
 * Filename is cleaned before being returned.
 *
 * @see #getCleanFileName(String)
 * @since 7.1
 */
public static String retrieveFilename(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 getCleanFileName(filename);
        }
    }
    return null;
}

From source file:org.grible.servlets.ServletHelper.java

public static 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(File.separator) + 1);
        }//from  www  .j a va  2s. co  m
    }
    return null;
}

From source file:de.unirostock.sems.caroweb.Converter.java

protected static final String extractFileName(Part part) {
    String header = part.getHeader("content-disposition");
    if (header == null)
        header = part.getHeader("Content-Disposition");
    if (header == null)
        header = part.getHeader("CONTENT-DISPOSITION");
    if (header == null) {
        LOGGER.error("cannot find CONTENT-DISPOSITION");
    }/*from  w  ww  .  j  a v a2s.  com*/

    String[] items = part.getHeader("content-disposition").split(";");
    for (String s : items)
        if (s.trim().startsWith("filename"))
            return s.substring(s.indexOf("=") + 2, s.length() - 1);
    return "container";
}

From source file:controllers.IndexServlet.java

private static void Upload(HttpServletRequest request, HttpServletResponse response, PrintWriter writer) {
    response.setContentType("text/plain");

    try {/*  ww  w  .j a  v  a2s  .  c o  m*/
        Part httpPostedFile = request.getPart("file");

        String fileName = "";
        for (String content : httpPostedFile.getHeader("content-disposition").split(";")) {
            if (content.trim().startsWith("filename")) {
                fileName = content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
            }
        }

        long curSize = httpPostedFile.getSize();
        if (DocumentManager.GetMaxFileSize() < curSize || curSize <= 0) {
            writer.write("{ \"error\": \"File size is incorrect\"}");
            return;
        }

        String curExt = FileUtility.GetFileExtension(fileName);
        if (!DocumentManager.GetFileExts().contains(curExt)) {
            writer.write("{ \"error\": \"File type is not supported\"}");
            return;
        }

        InputStream fileStream = httpPostedFile.getInputStream();

        fileName = DocumentManager.GetCorrectName(fileName);
        String fileStoragePath = DocumentManager.StoragePath(fileName, null);

        File file = new File(fileStoragePath);

        try (FileOutputStream out = new FileOutputStream(file)) {
            int read;
            final byte[] bytes = new byte[1024];
            while ((read = fileStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            out.flush();
        }

        writer.write("{ \"filename\": \"" + fileName + "\"}");

    } catch (IOException | ServletException e) {
        writer.write("{ \"error\": \"" + e.getMessage() + "\"}");
    }
}

From source file:com.joseflavio.uxiamarelo.servlet.UxiAmareloServlet.java

/**
 * @see Part#getSubmittedFileName()//from  ww w.j  a v a  2s .c om
 */
private static String getNome(Part arquivo, String codificacao) throws UnsupportedEncodingException {
    String nome = null;
    String content_disposition = arquivo.getHeader("content-disposition");
    if (content_disposition == null)
        return null;
    for (String p : content_disposition.split(";")) {
        p = p.trim();
        if (p.startsWith("filename")) {
            nome = p.substring(p.indexOf("=") + 2, p.length() - 1);
            break;
        }
    }
    if (nome != null) {
        nome = URLDecoder.decode(nome, codificacao);
        char sep = nome.indexOf('/') >= 0 ? '/' : nome.indexOf('\\') >= 0 ? '\\' : '#';
        if (sep != '#') {
            nome = nome.substring(nome.lastIndexOf(sep) + 1);
        }
    }
    return nome;
}