javaweb上传文件不成功
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
if(session.getAttribute("username")==null){
response.sendRedirect(Key.APPLICATION_NAME+"/index.jsp");
return;
}
response.setContentType("text/html;charset=UTF-8");
String cusId = request.getParameter("cusId");
String cusName = request.getParameter("cusName");
String petName = request.getParameter("petName");
String petBirthday = request.getParameter("petBirthday");
//说明输入的请求信息采用UTF-8编码方式
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
//Servlet3.0中新引入的方法,用来处理multipart/form-data类型编码的表单
Part part = request.getPart("petPhoto");
//获取HTTP头信息headerInfo=(form-data; name="file" filename="文件名")
String headerInfo = part.getHeader("content-disposition");
//从HTTP头信息中获取文件名fileName=(文件名)
String fileName = headerInfo.substring(headerInfo.lastIndexOf("=") + 2, headerInfo.length() - 1);
//获得存储上传文件的文件夹路径
String fileSavingFolder = this.getServletContext().getRealPath("/UpLoad");
System.out.println("文件保存路径:"+fileSavingFolder);
//获得存储上传文件的完整路径(文件夹路径+文件名)
String fileSavingPath = fileSavingFolder + File.separator + fileName;
System.out.println("fileSavingFolder:"+fileSavingFolder);
System.out.println("fileSavingPath:"+fileSavingPath);
//如果存储上传文件的文件夹不存在,则创建文件夹
File f = new File(fileSavingFolder + File.separator);
if(!f.exists()){
f.mkdirs();
}
//将上传的文件内容写入服务器文件中
part.write(fileSavingPath);
//输出上传成功信息
out.println("文件上传成功~!");
String petPhoto =fileSavingPath;
Pet pet=new Pet();
pet.setName(petName);
pet.setBirthdate(petBirthday);
pet.setPhoto(fileName);
pet.setOwnerId(Integer.parseInt(cusId));
try {
petDAO.save(pet);
} catch (Exception e) {
e.printStackTrace();
}
response.sendRedirect(APPLICATION_NAME+"/CustomerServlet?id="+cusId);
}
fileSavingPath:C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ph\UpLoad\C:\Users\25816\Desktop\新建文件夹\car.png
十二月 11, 2021 5:49:48 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [com.engintime.ph.servlet.PetServlet] in context with path [/ph] threw exception
java.io.IOException: java.io.FileNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ph\UpLoad\C:\Users\25816\Desktop\新建文件夹\car.png (文件名、目录名或卷标语法不正确。)
我有上传的文件夹但就是报错
希望能解决