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:net.voidfunction.rm.master.AdminServlet.java

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

From source file:com.imagelake.uploads.Servlet_Upload.java

/**
 * Utility method to get file name from HTTP header content-disposition
 *///from  w  w  w .j av a2  s  .  co  m
private String getFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    System.out.println("content-disposition header= " + contentDisp);
    System.out.println(contentDisp);
    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:codes.thischwa.c5c.DispatcherPUT.java

private String getFileName(final Part part) {
    final String partHeader = part.getHeader("content-disposition");
    for (String content : partHeader.split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
        }//from  w  ww.ja v  a 2s  .co m
    }
    return null;
}

From source file:org.primefaces.model.NativeUploadedFile.java

private String resolveFilename(Part part) {
    return FileUploadUtils
            .getValidFilename(getContentDispositionFileName(part.getHeader("content-disposition")));
}

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

private String getFileName(final Part part) {
    if (part == null) {
        return null;
    }/*  www. j  ava 2 s. c o m*/

    if (part.getContentType() == null) {
        return null;
    }

    String contentDisposition = part.getHeader("content-disposition");

    if (StringUtils.isEmpty(contentDisposition)) {
        return null;
    }
    // form-data; name="file1"; filename="TODO.txt"
    return StringUtils.substringBetween(contentDisposition, "filename=\"", "\"");

}

From source file:cn.mypandora.controller.MyUpload.java

/**
 * @param part ??null//from   w  ww.j a v  a2  s . c om
 * @return String
 * @Title: getFileName
 * @Description: PartHeader??????
 */
private String getFileName(Part part) {
    if (part == null) {
        return null;
    }
    // ?header?content-disposition??????
    String fileName = part.getHeader("content-disposition");
    if (StringUtils.isBlank(fileName)) {
        return null;
    }
    return StringUtils.substringBetween(fileName, "filename=\"", "\"");
}

From source file:gob.dp.simco.registro.controller.RegistroController.java

private static String getFilename(Part part) {
    if (part != null)
        try {/*from   ww  w .  j av  a2 s  .  c om*/
            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);
                }
            }
        } catch (Exception e) {
            log.error("ERROR - getFilename()" + e);
        }
    return null;
}

From source file:nz.net.paulo.FileUploadHandler.java

@Override
public void write(HttpServletResponse response) throws IOException {
    if (!rp.isMultiPart()) {
        throw new IllegalArgumentException(
                "Request is not multipart, please 'multipart/form-data' enctype for your form.");
    }//ww w .j  a  v a2s  .  c  o  m
    response.setContentType("application/json");
    JSONArray jsonArray = new JSONArray();
    String fileName = extractFileName(rp.getContentDisposition());
    Date lastModifiedDate = extractLastModifiedDate(rp.getContentDisposition());

    try {
        // we write all the parts to the temp dir...
        for (Part part : rp.getParts()) {
            if (StringUtils.isBlank(fileName)) {
                String partContentDisposition = part.getHeader("content-disposition");
                fileName = extractFileName(partContentDisposition);
                lastModifiedDate = extractLastModifiedDate(partContentDisposition);
            }
            String filePath = getFullFilePath(fileName);
            System.out.println("FilePath: " + filePath);
            part.write(filePath);
            if (isFilePart()) {
                File tempFile = new File(filePath);
                File partFile = rp.getPart(fileName);
                if (partFile.exists()) {
                    if (partFile.lastModified() != lastModifiedDate.getTime()) {
                        partFile.delete();
                        tempFile.delete();
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                "The file appears to have been modified since the last upload. Please try again.");
                        return;
                    }
                    partFile = mergeFiles(partFile, tempFile);
                } else {
                    tempFile.renameTo(partFile);
                }
                partFile.setLastModified(lastModifiedDate.getTime());
                if (partFile.length() == getFileSize(rp.getContentRange())) {
                    File finalFile = rp.getFileInUploadDir(fileName);
                    partFile.renameTo(finalFile);
                }
            }
            File mainFile = rp.getFileInUploadDir(fileName);
            if (mainFile.exists()) {
                jsonArray.put(getFileJson(mainFile.getName(), mainFile.length()));
            }
        }
        String responseString = "{\"files\": " + jsonArray.toString() + "}";
        try (PrintWriter writer = response.getWriter()) {
            writer.write(responseString);
        }
    } catch (IllegalStateException | ServletException e) {
        throw new IOException(e);
    }

}

From source file:be.thomasmore.controller.FileController.java

private String getFileName(Part part) {
    final String partHeader = part.getHeader("content-disposition");
    System.out.println("***** partHeader: " + partHeader);
    for (String content : part.getHeader("content-disposition").split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
        }/*from  ww w  .ja va 2 s  .c  o m*/
    }
    return null;
}

From source file:lc.kra.servlet.FileManagerServlet.java

private String partFileName(Part part) {
    String header, file = null;//w  ww .j a  v  a2  s.  com
    if ((header = part.getHeader("content-disposition")) != null) {
        String lowerHeader = header.toLowerCase(Locale.ENGLISH);
        if (lowerHeader.startsWith("form-data") || lowerHeader.startsWith("attachment")) {
            ParameterParser parser = new ParameterParser();
            parser.setLowerCaseNames(true);
            Map<String, String> parameters = parser.parse(header, ';');
            if (parameters.containsKey("filename"))
                file = (file = parameters.get("filename")) != null ? file.trim() : "";
        }
    }
    return file;
}