Java Code Examples for javax.servlet.http.Part#getHeader()

The following examples show how to use javax.servlet.http.Part#getHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardMultipartHttpServletRequest.java    From spring-analysis-note with MIT License 10 votes vote down vote up
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);
	}
}
 
Example 2
Source File: StandardMultipartHttpServletRequest.java    From lams with GNU General Public License v2.0 7 votes vote down vote up
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);
	}
}
 
Example 3
Source File: StandardMultipartHttpServletRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
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);
	}
}
 
Example 4
Source File: StandardMultipartHttpServletRequest.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 5
Source File: FileUploadUtils.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: OlamiController.java    From silk2asr with Apache License 2.0 5 votes vote down vote up
/** 
    * 从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 "";  
   }
 
Example 7
Source File: UploadServlet.java    From psychoPATH with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 "";
}
 
Example 8
Source File: UploadServlet.java    From psychoPATH with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 "";
}
 
Example 9
Source File: UploadServlet.java    From psychoPATH with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 "";
}
 
Example 10
Source File: FileUploadServlet.java    From journaldev with MIT License 5 votes vote down vote up
/**
 * 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 "";
}
 
Example 11
Source File: ListenHTTPServlet.java    From nifi with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: HandleHttpRequest.java    From nifi with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: Servlet4Upload.java    From boubei-tss with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 14
Source File: UploadUtils.java    From HotelSystem with Apache License 2.0 3 votes vote down vote up
/**
 * 用于上传一个文件,返回该文件的文件名
 *
 * @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;
}