图片虚拟目录--即图片保存在window硬盘上面

这个是图片保存在电脑的硬盘上面的图片上传设置,既不是在web工程中,也不是在专门的图片服务器中,下面是配置方法:

r

这里的Document base 我们这里设置为F:images

如果在浏览器访问图片访问虚拟目录即可:

http://localhost:8088/pic/40c43d9a-6e48-43ce-976d-cc0147216f13.jpg

 此时我们查看设置的图片路径:就在我们的电脑F盘有这些图片,上传成功。

具体代码实现:

@RequestMapping("/editSubmit")
	public String editSubmit(Model model ,Integer id,Items item,MultipartFile pictureFile) throws IllegalStateException, IOException{
		model.addAttribute("id",id);
		//MultipartFile专门用来接收上传的图片,附件等等类型复杂文件
		if (pictureFile!=null) {
			String originalFilename=pictureFile.getOriginalFilename();
			String filePath="F:\images\";
			/**
			 * 在处理 UNIX 平台的根目录,以及 Microsoft Windows 平台的盘符、根目录和 UNC(网络硬盘)
			 * 路径名时,将用到前缀这一概念。如下所示:
			 * 
			 * 对于 UNIX 平台,绝对路径名的前缀始终是 "/"。相对路径名没有前缀。
			 * 表示根目录的绝对路径名的前缀为 "/" 且名称序列为空。
			 * 对于 Microsoft Windows 平台,包含盘符的路径名前缀由驱动器号和一个 ":"
			 * 组成。如果路径名是绝对路径名,还可能后跟 "\"。
			 * UNC 路径名的前缀是 "\\";主机名和共享名是名称序列中的前两个名称。没有指定驱动器的相对路径名没有前缀。
			 * 
			 * / String filePath="F:\images\"; String
			 * originalFilename=pictureFile.getOriginalFilename(); /* 关于字符串的截取:
			 * int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。 int indexOf(int ch,
			 * int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 int
			 * indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。 int indexOf(String
			 * str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 int
			 * lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。 int lastIndexOf(int
			 * ch, int fromIndex) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 int
			 * lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。 int
			 * lastIndexOf(String str, int fromIndex)
			 * 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 substring(int beginIndex)
			 * 返回一个新的字符串,它是此字符串的一个子字符串。 substring(int beginIndex, int endIndex)
			 * 返回一个新字符串,它是此字符串的一个子字符串。
			 * 索引就是指的查找的该字符在此字符串中的哪个位置:originalFilename.lastIndexOf(".")这里得到的是.
			 * 的位置是9
			 * originalFilename.substring(originalFilename.lastIndexOf("."))
			 * 这个是substring截取字符串,从第九个位置开始,即截取.jpg这个串。
			 * 
			 */
		
			String newname=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
		//图片全路径设置
			File file=new File(filePath+newname);
			//将内存中的文件写入磁盘
			pictureFile.transferTo(file);
			//将新图片的名称写入数据库
			item.setPic(newname);
			
		}
		itemService.update(id,item);	
		return "redirect:queryitem.action";
	}
	//这里要区分开jsp参数与controller参数的绑定形式:是restful形式还是非restful形式的绑定。如果是restful形式的用{},用@Pathvariable接收
	//如果是非restful形式的,则使用在方法的参数列表中接收
	@RequestMapping("/deleteitem")
	public String deleteitem(Integer id) {
		itemService.deleteItem(id);
		return "redirect:queryitem.action";
	}
	

  这里注意File类的路径在windows中是\.

     另外注意字符串的截取问题

原文地址:https://www.cnblogs.com/fengli9998/p/6649554.html