web中实现文件上传出错
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="javax.servlet.http.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
try{
ServletInputStream in=request.getInputStream();
int len=request.getContentLength();
byte []b=new byte[350];
int i=0;
i=in.readLine(b,0,128);
len-=i;
i=in.readLine(b,0,128);
len-=i;
String s=new String(b,0,i);
String filename=s.substring(s.lastIndexOf("=")+2,s.lastIndexOf("\""));
if(!filename.equals("")) {
String saveName=filename.substring(filename.lastIndexOf("\\")+1);
i=in.readLine(b,0,128);
len-=i;
i=in.readLine(b,0,128);
len-=i;
File save=new File("d"+File.separator+saveName);
if(!save.exists()){
save.createNewFile(); //为什么这句话会出问题
}
FileOutputStream out1=new FileOutputStream(save);
while((i=in.readLine(b,0,128))!=-1&&len>47){
len-=i;
out1.write(b,0,i) ;
}
in.close();
out1.close();
out.print("<script>alert('上传成功');location.href='file1.jsp';</script>");
} else{ out.print("<script>alert('没有找到文件');location.href='file1.jsp';</script>");
}
}
catch(Exception e){System.out.println(e);}
%>
</body>
</html>
上面是在jsp页面操作文件上传,另一个上传的jsp画面省略了,运行省略的画面,选择上传的文件,会跳转到
此页面,捕捉了异常,输出“
java.io.IOException: 系统找不到指定的路径”,出错的地方是save.createNewFile();
这儿语法没错误,为什么会出现此种情况????
------解决方案--------------------
File save=new File("d"+File.separator+saveName);
这句话错了。。。。是d: