请高手帮忙看看,为什么上传的文件和PDF文件夹一重启就不见了,再谢麻烦改成永久保存的,谢谢
public String addPdf() throws IOException{

    String targetDirectory=ServletActionContext.getServletContext().getRealPath("/pdf");
    String targetFileName=generateFileName(fileName);
File target=new File(targetDirectory,targetFileName);
FileUtils.copyFile(doc,target);
System.out.println(target.getName());
String str="update Employees as e set e.pdf=? where e.empId=?";
String str2=request.getParameter("empId");
String str4=request.getParameter("gaiyao");
String str1=","+str4+":pdf/"+targetFileName;
employeesbiz.update(str,str1,str2);
//String str3="from Employees as e where e.empId=?";
//Employees empdg=(Employees) employeesbiz.serch(str3,str2);
   //session.put("empId", empdg);
request.setAttribute("empId", str2);
return "success";
     }

解决方案 »

  1.   

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(fileupload));//fileupload上传的文件流String="/tmp"
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    byte[] buffer = new byte[1024];
    int len;
    while ((len = fis.read(buffer)) > 0) {
     fos.write(buffer, 0, len);// 写入文件
    }
      

  2.   

    FileUtils.copyFile(doc,target);
    不是已经复制过来了吗???
      

  3.   

    File target=new File(targetDirectory,targetFileName);
    把targetDirectory改成一个固定位置不就好了!另外,保存在工程目录下也不会丢失的,会丢失有两种情况:
    1、基于开发工具启动时,运行期目录被开发工具重置了;
    2、TOMCAT使用WAR包部署,TOMCAT启动的时候会重新以WAR包的内容运行,你的文件就么了!