前两天把win系统里面的网站项目源代码转移到mac电脑后发现,上传图片的功能用不了,一直报错,研究了一下才发现,win系统和mac系统的目录路径不同导致的。在win上是”\“,而在mac上确是”/“。
这是win系统上的代码
1 2 3 4 5 6 7
| if(100<size || !fileType.equals("jpg")){ out.print("<script language='javascript'>alert('上传的图片文件必须小于100KB,且为jpg格式');window.location.href='javascript:history.go(-1)';</script>"); } else{ String fileName = name.substring(name.lastIndexOf('\\')+1,name.length()); String ctxpath =this.getServletContext().getRealPath(""); String photopath = ctxpath.substring(0,ctxpath.lastIndexOf("\\"))+"\\touxiang"; File uploadedFile = new File(photopath,user+".jpg");
|
一开始转到mac系统中时,报错。
将”\“改成”"
1 2 3 4
| String fileName = name.substring(name.lastIndexOf('/')+1,name.length()); String ctxipath =this.getServletContext().getRealPath(""); String photopath = ctxpath.substring(0,ctxpath.lastIndexOf("/"))+"/touxiang"; File uploadedFile = new File(photopath,user+".jpg");
|
测试成功上传文件
