java.io.FileNotFoundException: XXX.jpg(系统找不到指定的文件。)怎么解决
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String uploadPath = "D:\\temp\\";//上传文件的目录
private String tempPath = "D:\\temp\\buffer";//临时文件目录
File tempPathFile;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
this.doPost(req, resp);
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
request.setCharacterEncoding("gbk");
String action = request.getParameter("action");
if(action == null){//当action为空时
request.getRequestDispatcher("error.jsp").forward(request,response);
return;
}
if(action.equals("insertHeadImage")){//上传头像动作
String tdis = request.getParameter("tdis");
if(tdis == null){
tdis = "";
}
HttpSession session=request.getSession(true);
String uid = (String) session.getAttribute("uid");
try {
//实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);//设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);//设置缓冲区目录
//用以上工厂实例化上传组件
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(3*1024*1024);//设置最大文件尺寸,这里是3MB
List fileitems = upload.parseRequest(request);//得到所有的文件
Iterator i = fileitems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null){
if(!fileName.equals(""))
{
File fullFile = new File(fi.getName());
DBUtil.insertDCMS_head(tdis, fullFile, Integer.parseInt(uid));//存储到数据库
//File savedFile = new File(uploadPath, fullFile.getName());
//fi.write(savedFile);
}
}
}
request.getRequestDispatcher("changeHead.jsp").forward(request,response);
} catch (Exception e) {//可以跳转出错页面
e.printStackTrace();//打印异常信息
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}