計算upload路徑

ZHOU_VIP發表於2018-09-26
/**
 * 計算upload路徑
 * 
 * @param request
 * @return
 */
private String getUploadPath(HttpServletRequest request){
	String realPath = request.getSession().getServletContext().getRealPath(request.getRequestURI());
	String basePath = request.getContextPath();
	basePath = basePath.replaceAll("/", "");
	logger.info("realPath="+realPath+", basePath="+basePath);
	int index = realPath.indexOf(basePath);
	if(index >= 0){
		realPath = realPath.substring(0, index);
	}
	realPath = realPath.replaceAll("\\\\", "/");
	if(!realPath.endsWith("/")){
		realPath = realPath + "/";
	}
	String filePath = realPath+"tmpUpload/";
	File f = new File(filePath);
	if(f.exists() == false){
		f.mkdirs();
	}
	f = null;
	return filePath;
}

 

相關文章