Struts1.2文件上传注意的问题

来源:互联网 发布:淘宝店找不到仓库 编辑:程序博客网 时间:2024/05/17 21:48

//camera     文件夹名

FormFile ff=gs.getFile();
   String path=request.getSession().getServletContext().getRealPath("//")+"camera//"+ff.getFileName();
   System.out.println(path);
      File director=new File(path);
//      if(!director.exists()){
//       director.mkdir();
//     问题就在此处,假如这个path不存在他就自动创建这个目录(注意是目录而不是文件)前提是camera已经存在。此处对大多数新手带来困惑,不然会报java.io.FileNotFoundException: D:/xxx/yyy (拒绝访问。)错误 ,解决方法是创建表文件而不是目录,例director.createNewFile();即可。}
   try {
    FileOutputStream fos=new FileOutputStream(director);
    fos.write(ff.getFileData());
    fos.flush();
    fos.close();
   } catch (FileNotFoundException e) {
    
    e.printStackTrace();
   } catch (IOException e) {
    
    e.printStackTrace();
   }
  
  gs.setPicture("camera//"+ff.getFileName());

原创粉丝点击