下面列出了javax.servlet.http.Part#getHeaderNames() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
if (part != null) {
HttpHeaders headers = new HttpHeaders();
for (String headerName : part.getHeaderNames()) {
headers.put(headerName, new ArrayList<>(part.getHeaders(headerName)));
}
return headers;
}
else {
return null;
}
}
catch (Throwable ex) {
throw new MultipartException("Could not access multipart servlet request", ex);
}
}
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
try {
Collection<Part> parts = req.getParts();
PrintWriter writer = resp.getWriter();
writer.println("PARAMS:");
for (Part part : parts) {
writer.println("name: " + part.getName());
writer.println("filename: " + part.getSubmittedFileName());
writer.println("content-type: " + part.getContentType());
Collection<String> headerNames = new TreeSet<>(part.getHeaderNames());
for (String header : headerNames) {
writer.println(header + ": " + part.getHeader(header));
}
writer.println("size: " + part.getSize());
writer.println("content: " + FileUtils.readFile(part.getInputStream()));
}
} catch (Exception e) {
resp.getWriter().write("EXCEPTION: " + e.getClass());
}
}
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
if (part != null) {
HttpHeaders headers = new HttpHeaders();
for (String headerName : part.getHeaderNames()) {
headers.put(headerName, new ArrayList<>(part.getHeaders(headerName)));
}
return headers;
}
else {
return null;
}
}
catch (Throwable ex) {
throw new MultipartException("Could not access multipart servlet request", ex);
}
}
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
if (part != null) {
HttpHeaders headers = new HttpHeaders();
for (String headerName : part.getHeaderNames()) {
headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
}
return headers;
}
else {
return null;
}
}
catch (Throwable ex) {
throw new MultipartException("Could not access multipart servlet request", ex);
}
}
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
try {
Part part = getPart(paramOrFileName);
if (part != null) {
HttpHeaders headers = new HttpHeaders();
for (String headerName : part.getHeaderNames()) {
headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
}
return headers;
}
else {
return null;
}
}
catch (Exception ex) {
throw new MultipartException("Could not access multipart servlet request", ex);
}
}
private FlowFile savePartDetailsAsAttributes(final ProcessSession session, final Part part, final FlowFile flowFile, final int sequenceNumber, final int allPartsCount) {
final Map<String, String> attributes = new HashMap<>();
for (String headerName : part.getHeaderNames()) {
final String headerValue = part.getHeader(headerName);
putAttribute(attributes, "http.headers.multipart." + headerName, headerValue);
}
putAttribute(attributes, "http.multipart.size", part.getSize());
putAttribute(attributes, "http.multipart.content.type", part.getContentType());
putAttribute(attributes, "http.multipart.name", part.getName());
putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName());
putAttribute(attributes, "http.multipart.fragments.sequence.number", sequenceNumber + 1);
putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount);
return session.putAllAttributes(flowFile, attributes);
}
private FlowFile savePartAttributes(ProcessContext context, ProcessSession session, Part part, FlowFile flowFile, final int i, final int allPartsCount) {
final Map<String, String> attributes = new HashMap<>();
for (String headerName : part.getHeaderNames()) {
final String headerValue = part.getHeader(headerName);
putAttribute(attributes, "http.headers.multipart." + headerName, headerValue);
}
putAttribute(attributes, "http.multipart.size", part.getSize());
putAttribute(attributes, "http.multipart.content.type", part.getContentType());
putAttribute(attributes, "http.multipart.name", part.getName());
putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName());
putAttribute(attributes, "http.multipart.fragments.sequence.number", i+1);
putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount);
return session.putAllAttributes(flowFile, attributes);
}
@ActionMethod(portletName = "MultipartPortlet")
public void handleDialog(ActionRequest req, ActionResponse resp) throws IOException, PortletException {
List<String> lines = new ArrayList<String>();
req.getPortletSession().setAttribute("lines", lines);
lines.add("handling dialog");
StringBuilder txt = new StringBuilder(128);
String clr = req.getActionParameters().getValue("color");
txt.append("Color: ").append(clr);
lines.add(txt.toString());
logger.debug(txt.toString());
resp.getRenderParameters().setValue("color", clr);
txt.setLength(0);
Part part = null;
try {
part = req.getPart("file");
} catch (Throwable t) {}
if ((part != null) && (part.getSubmittedFileName() != null) &&
(part.getSubmittedFileName().length() > 0)) {
txt.append("Uploaded file name: ").append(part.getSubmittedFileName());
txt.append(", part name: ").append(part.getName());
txt.append(", size: ").append(part.getSize());
txt.append(", content type: ").append(part.getContentType());
lines.add(txt.toString());
logger.debug(txt.toString());
txt.setLength(0);
txt.append("Headers: ");
String sep = "";
for (String hdrname : part.getHeaderNames()) {
txt.append(sep).append(hdrname).append("=").append(part.getHeaders(hdrname));
sep = ", ";
}
lines.add(txt.toString());
logger.debug(txt.toString());
// Store the file in a temporary location in the webapp where it can be served.
try {
String fn = part.getSubmittedFileName();
String ct = part.getContentType();
if (ct != null && (ct.equals("text/plain") || ct.matches("image/(?:png|gif|jpg|jpeg)"))) {
String ext = ct.replaceAll("\\w+/", "");
lines.add("determined extension " + ext + " from content type " + ct);
File img = getFile();
if (img.exists()) {
lines.add("deleting existing temp file: " + img.getCanonicalPath());
img.delete();
}
InputStream is = part.getInputStream();
Files.copy(is, img.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
lines.add("Bad file type. Must be plain text or image (gif, jpeg, png).");
}
resp.getRenderParameters().setValue("fn", fn);
resp.getRenderParameters().setValue("ct", ct);
} catch (Exception e) {
lines.add("Exception doing I/O: " + e.toString());
txt.setLength(0);
txt.append("Problem getting temp file: " + e.getMessage() + "\n");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
txt.append(sw.toString());
logger.warn(txt.toString());
}
} else {
lines.add("file part was null");
}
}