javax.servlet.http.Part#getHeader()源码实例Demo

下面列出了javax.servlet.http.Part#getHeader() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private void parseRequest(HttpServletRequest request) {
	try {
		Collection<Part> parts = request.getParts();
		this.multipartParameterNames = new LinkedHashSet<>(parts.size());
		MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
		for (Part part : parts) {
			String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
			ContentDisposition disposition = ContentDisposition.parse(headerValue);
			String filename = disposition.getFilename();
			if (filename != null) {
				if (filename.startsWith("=?") && filename.endsWith("?=")) {
					filename = MimeDelegate.decode(filename);
				}
				files.add(part.getName(), new StandardMultipartFile(part, filename));
			}
			else {
				this.multipartParameterNames.add(part.getName());
			}
		}
		setMultipartFiles(files);
	}
	catch (Throwable ex) {
		handleParseFailure(ex);
	}
}
 
private void parseRequest(HttpServletRequest request) {
	try {
		Collection<Part> parts = request.getParts();
		this.multipartParameterNames = new LinkedHashSet<>(parts.size());
		MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
		for (Part part : parts) {
			String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
			ContentDisposition disposition = ContentDisposition.parse(headerValue);
			String filename = disposition.getFilename();
			if (filename != null) {
				if (filename.startsWith("=?") && filename.endsWith("?=")) {
					filename = MimeDelegate.decode(filename);
				}
				files.add(part.getName(), new StandardMultipartFile(part, filename));
			}
			else {
				this.multipartParameterNames.add(part.getName());
			}
		}
		setMultipartFiles(files);
	}
	catch (Throwable ex) {
		handleParseFailure(ex);
	}
}
 
private void parseRequest(HttpServletRequest request) {
	try {
		Collection<Part> parts = request.getParts();
		this.multipartParameterNames = new LinkedHashSet<String>(parts.size());
		MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size());
		for (Part part : parts) {
			String disposition = part.getHeader(CONTENT_DISPOSITION);
			String filename = extractFilename(disposition);
			if (filename == null) {
				filename = extractFilenameWithCharset(disposition);
			}
			if (filename != null) {
				files.add(part.getName(), new StandardMultipartFile(part, filename));
			}
			else {
				this.multipartParameterNames.add(part.getName());
			}
		}
		setMultipartFiles(files);
	}
	catch (Throwable ex) {
		throw new MultipartException("Could not parse multipart servlet request", ex);
	}
}
 
private void parseRequest(HttpServletRequest request) {
	try {
		Collection<Part> parts = request.getParts();
		this.multipartParameterNames = new LinkedHashSet<String>(parts.size());
		MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size());
		for (Part part : parts) {
			String disposition = part.getHeader(CONTENT_DISPOSITION);
			String filename = extractFilename(disposition);
			if (filename == null) {
				filename = extractFilenameWithCharset(disposition);
			}
			if (filename != null) {
				files.add(part.getName(), new StandardMultipartFile(part, filename));
			}
			else {
				this.multipartParameterNames.add(part.getName());
			}
		}
		setMultipartFiles(files);
	}
	catch (Exception ex) {
		throw new MultipartException("Could not parse multipart servlet request", ex);
	}
}
 
源代码5 项目: molgenis   文件: FileUploadUtils.java
/**
 * Get the filename of an uploaded file
 *
 * @return the filename or null if not present
 */
public static String getOriginalFileName(Part part) {
  String contentDisposition = part.getHeader("content-disposition");
  if (contentDisposition != null) {
    for (String cd : contentDisposition.split(";")) {

      if (cd.trim().startsWith("filename")) {
        String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim();
        Path filename = Paths.get(path).getFileName();
        return StringUtils.hasText(filename.toString()) ? filename.toString() : null;
      }
    }
  }

  return null;
}
 
源代码6 项目: silk2asr   文件: OlamiController.java
/** 
    * 从content-disposition头中获取源文件名 
    *  
    * content-disposition头的格式如下: 
    * form-data; name="dataFile"; filename="PHOTO.JPG" 
    *  
    * @param part 
    * @return 
    */
private String extractFileName(Part part) {  
       String contentDisp = part.getHeader("content-disposition");  
       String[] items = contentDisp.split(";");  
       for (String s : items) {  
           if (s.trim().startsWith("filename")) {  
               return s.substring(s.indexOf("=") + 2, s.length()-1);  
           }  
       }  
       return "";  
   }
 
源代码7 项目: psychoPATH   文件: UploadServlet.java
/**
 * Extracts file name from HTTP header content-disposition
 */
private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}
 
源代码8 项目: psychoPATH   文件: UploadServlet.java
/**
 * Extracts file name from HTTP header content-disposition
 */
private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}
 
源代码9 项目: psychoPATH   文件: UploadServlet.java
/**
 * Extracts file name from HTTP header content-disposition
 */
private String extractFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    String[] items = contentDisp.split(";");
    for (String s : items) {
        if (s.trim().startsWith("filename")) {
            return s.substring(s.indexOf("=") + 2, s.length()-1);
        }
    }
    return "";
}
 
源代码10 项目: journaldev   文件: FileUploadServlet.java
/**
 * Utility method to get file name from HTTP header content-disposition
 */
private String getFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    System.out.println("content-disposition header= "+contentDisp);
    String[] tokens = contentDisp.split(";");
    for (String token : tokens) {
        if (token.trim().startsWith("filename")) {
            return token.substring(token.indexOf("=") + 2, token.length()-1);
        }
    }
    return "";
}
 
源代码11 项目: nifi   文件: ListenHTTPServlet.java
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);
}
 
源代码12 项目: nifi   文件: HandleHttpRequest.java
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);
}
 
源代码13 项目: boubei-tss   文件: Servlet4Upload.java
String doUpload(HttpServletRequest request, Part part) throws Exception {
	
	/* 
	 * gets absolute path of the web application, tomcat7/webapps/tss
	String defaultUploadPath = request.getServletContext().getRealPath("");
	 */
	
	String uploadPath = DMUtil.getExportPath() + File.separator + "upload";
       FileHelper.createDir(uploadPath);

	// 获取上传的文件真实名字(含后缀)
	String contentDisp = part.getHeader("content-disposition");
	String orignFileName = "";
	String[] items = contentDisp.split(";");
	for (String item : items) {
		if (item.trim().startsWith("filename")) {
			orignFileName = item.substring(item.indexOf("=") + 2, item.length() - 1);
			break;
		}
	}
	
	String subfix = FileHelper.getFileSuffix(orignFileName), newFileName;
	
	// 允许使用原文件名
	String useOrignName = request.getParameter("useOrignName");
	if(useOrignName != null) {
		newFileName = orignFileName;
	} else {
		newFileName = System.currentTimeMillis() + "." + subfix; // 重命名
	}
	
       String newFilePath = uploadPath + File.separator + newFileName;
       
       // 自定义输出到指定目录
	InputStream is = part.getInputStream();
	FileOutputStream fos = new FileOutputStream(newFilePath);
	int data = is.read();
	while(data != -1) {
	  fos.write(data);
	  data = is.read();
	}
	fos.close();
	is.close();
	
	String afterUploadClass = request.getParameter("afterUploadClass");
	AfterUpload afterUpload = (AfterUpload) BeanUtil.newInstanceByName(afterUploadClass);
	
	String jsCallback = EasyUtils.obj2String( request.getParameter("callback") );
	return afterUpload.processUploadFile(request, newFilePath, orignFileName) + jsCallback;
}
 
源代码14 项目: HotelSystem   文件: UploadUtils.java
/**
 * 用于上传一个文件,返回该文件的文件名
 *
 * @return String 存储的文件名
 * @name
 * @notice none
 * @author <a href="mailto:[email protected]">黄钰朝</a>
 * @date 2019/4/19
 */
public static String upload(Part part) throws IOException {
    String head = part.getHeader("Content-Disposition");
    String filename = getUUID() + head.substring(head.lastIndexOf("."), head.lastIndexOf("\""));
    part.write(filename);
    return filename;
}