Obtener el nombre del archivo lastIndexOf ()

@RequestMapping("/savedrilladdition")
@ResponseBody
public DataResult saveDrillAddition(@RequestParam(value="imp_file", required =false) CommonsMultipartFile file,HttpServletRequest request) throws Exception {
	
	List<DrillAdditionBean> data = new ArrayList<DrillAdditionBean>();
	
	String projectid 		= ReactUtil.getStrParamDef(request, "projectid", "");
	String drillid 			= ReactUtil.getStrParamDef(request, "drillid", "");
	String drilladditionid	        = ReactUtil.getStrParamDef(request, "drilladditionid", "");
	String drilladditionname	= ReactUtil.getStrParamDef(request, "drilladditionname", "");
	int drilladditiontype		= Integer.parseInt(ReactUtil.getStrParamDef(request, "drilladditiontype", "1"));
	
	String drilladditionuploader	= ReactUtil.getStrParamDef(request, "drilladditionuploaderid", "");
	String drilladditionuploadtime	= ReactUtil.getStrParamDef(request, "drilladditionuploadtime", "");
	String drilladditionremark	= ReactUtil.getStrParamDef(request, "drilladditionremark", "");
	String drilladditionopflag 	= ReactUtil.getStrParamDef(request, "drilladditionopflag", "1");
	
	
	drilladditionid = generateId(projectid, ID_HEAD_EMERGENCY_DRILL_ADDITION);
		if (!checkAccessRight(Constants.OPCODE_EMERGENCY_DRILL_MOD)) {
			this.addSmLog(MODULE_NAME, SmLogBean.OP_RET_FAIL, "修改权限不足", "尝试增加应急预案演练记录", "");
			throw this.genNoRightException();
	}
	
	byte[] imagefile = null;
	String photoPath = "";
	String filename = "";
	if(file != null){
		imagefile = Stream2byte(file.getInputStream());
		//20190717094506
		String dtStrF = dateFormat.format(new Date());
		//D:/apache-tomcat-8.5.32/webapps/InfiComboV2Web/
		String filePath = this.getWebAppBasePath(request);
		String parentFilePath = (new File(filePath)).getParent();//D:\apache-tomcat-8.5.32\webapps
		filePath = parentFilePath+"/"+DRILL_ADDITION_PATH+"/"+dtStrF+"/";			
		String photoName = file.getOriginalFilename();//xini.jpg
		//filename = photoName.split("\\.")[0];假如遇到3.9计划.doc就有问题了,只取到了3
		filename = photoName.substring(0,photoName.lastIndexOf("."));
		filePath = filePath + photoName;//D:\apache-tomcat-8.5.32\webapps/photos/20190717094506/xini.jpg
		filePath = filePath.replaceAll("\\\\", "/");//D:/apache-tomcat-8.5.32/webapps/photos/20190717094506/xini.jpg
		File tmpDir = new File(filePath);
		if(!tmpDir.exists()){
			tmpDir.mkdirs();
		}
		// 上传
		File f = new File(filePath);
		file.transferTo(f);
		//相对路径:photos/20190717090218/xini.jpg
		photoPath = DRILL_ADDITION_PATH+"/"+dtStrF+"/"+photoName;		
	 }
	
	DrillAdditionBean addi = new DrillAdditionBean();
	
	addi.setProjectid(projectid);
	addi.setDrillid(drillid);
	addi.setDrilladditionid(drilladditionid);
	if(drilladditionname == null || drilladditionname.equals("")){
		addi.setDrilladditionname(filename);
	}else{
		addi.setDrilladditionname(drilladditionname);
	}
	addi.setDrilladditionremark(drilladditionremark);
	addi.setDrilladditiontype(drilladditiontype);
	addi.setDrilladditionuploader(drilladditionuploader);
	addi.setDrilladditionuploadtime(DateUtil.parseDatetime(drilladditionuploadtime));
	addi.setImagefile(imagefile);//附件
	addi.setDrilladditionurl(photoPath);//路径

	preplanService.addDrillAddition(addi);
	return ReactUtil.formatResult(0, "增加成功", data);	
	
}

Ejemplos: 

String name = "upload.doc";
// 获取后缀名
String sname = name.substring(name.lastIindexOf("."));
//获得upload部分
String fileName=name.substring(0,name.lastindexOf("."));

 

Publicados 926 artículos originales · ganado elogios 105 · vistas 760 000 +

Supongo que te gusta

Origin blog.csdn.net/ZHOU_VIP/article/details/105144352
Recomendado
Clasificación