项目路径,目的是读取到uploadfiles中的文件
代码:
for(String str:allFiles){
String[] ary=str.split(":");
File f=new File(ary[0]);//ary[0]:uploadfiels/text.txt
FileInputStream fis=new FileInputStream(f);//此处debug模式没问题,正常启动报错,文件找不到,路径错误
zos.putNextEntry(new ZipEntry(ary[1]));
int j=0;
byte[]  buffer=new byte[1024];
while((j=fis.read(buffer))>0){
zos.write(buffer,0,j);
}
fis.close();
}
求大神指点一二,debug模式和正常启动难道有什么区别么?

解决方案 »

  1.   

    ary[0]:uploadfiels/text.txt
    错了吧?
    ary[0]:uploadfiles/text.txt
      

  2.   

    额,这个是手误,就是说是相对路径,debug模式下可以,正常启动就提示文件找不到,我写成绝对路径就可以,求解
      

  3.   

    你还是把ary[0]打印一下看下吧。另外你把请求的代码发一些
      

  4.   

    你还是把ary[0]打印一下看下吧。另外你把请求的代码发一些
    <a href="app?service=page/activities.annotation.AnnotationQuickNew&action=batchDown">打包下载</a>发送请求,恩,ary[0]是不会错的,是uploadfiles/text.txt,debug模式下可以实现压缩文件下载
      

  5.   

    你还是把ary[0]打印一下看下吧。另外你把请求的代码发一些
    <a href="app?service=page/activities.annotation.AnnotationQuickNew&action=batchDown">打包下载</a>发送请求,恩,ary[0]是不会错的,是uploadfiles/text.txt,debug模式下可以实现压缩文件下载action发一下
      

  6.   

    你还是把ary[0]打印一下看下吧。另外你把请求的代码发一些
    <a href="app?service=page/activities.annotation.AnnotationQuickNew&action=batchDown">打包下载</a>发送请求,恩,ary[0]是不会错的,是uploadfiles/text.txt,debug模式下可以实现压缩文件下载action发一下
    String zipPath=cycle.getRequestContext().getRequest().getSession().getServletContext().getRealPath("/")+"/temp/"+
    new SimpleDateFormat("yyyy-MM-dd hh时mm分ss秒").format(new Date())+".zip";
    ZipOutputStream zos;
    try{
    zos=new ZipOutputStream(new FileOutputStream(new File(zipPath)));
    zos.setEncoding("gb2312");
     /*allFiles是在页面加载时候就填充好的,是文件地址:文件名的字符串,
    如uploadfiles/text.txt:文本.txt,现在的需求就是把这些文件压缩成压缩包,然后下载**/
    for(String str:allFiles){
    String[] ary=str.split(":");
    File f=new File(ary[0]);
    FileInputStream fis=new FileInputStream(f);//还是老问题,debug模式下可以正常运行,下载,退出debug模式这行就会报错
    zos.putNextEntry(new ZipEntry(ary[1]));
    int j=0;
    byte[]  buffer=new byte[1024];
    while((j=fis.read(buffer))>0){
    zos.write(buffer,0,j);
    }
    fis.close();
    }
    zos.close();
    File zipFile=new File(zipPath);
    HttpServletResponse resp=cycle.getRequestContext().getResponse();
    downZip(zipFile,resp);,是在不行的话我就想获得uploadfiles这个文件夹绝对路径好了,请问怎么获得呢
      

  7.   


    ServletContext context=getServletConfig().getServletContext();//获取上下文
    String path=context.getRealPath("uploadfiles");
    你还是把ary[0]打印一下看下吧。另外你把请求的代码发一些
    <a href="app?service=page/activities.annotation.AnnotationQuickNew&action=batchDown">打包下载</a>发送请求,恩,ary[0]是不会错的,是uploadfiles/text.txt,debug模式下可以实现压缩文件下载action发一下
    String zipPath=cycle.getRequestContext().getRequest().getSession().getServletContext().getRealPath("/")+"/temp/"+
    new SimpleDateFormat("yyyy-MM-dd hh时mm分ss秒").format(new Date())+".zip";
    ZipOutputStream zos;
    try{
    zos=new ZipOutputStream(new FileOutputStream(new File(zipPath)));
    zos.setEncoding("gb2312");
     /*allFiles是在页面加载时候就填充好的,是文件地址:文件名的字符串,
    如uploadfiles/text.txt:文本.txt,现在的需求就是把这些文件压缩成压缩包,然后下载**/
    for(String str:allFiles){
    String[] ary=str.split(":");
    File f=new File(ary[0]);
    FileInputStream fis=new FileInputStream(f);//还是老问题,debug模式下可以正常运行,下载,退出debug模式这行就会报错
    zos.putNextEntry(new ZipEntry(ary[1]));
    int j=0;
    byte[]  buffer=new byte[1024];
    while((j=fis.read(buffer))>0){
    zos.write(buffer,0,j);
    }
    fis.close();
    }
    zos.close();
    File zipFile=new File(zipPath);
    HttpServletResponse resp=cycle.getRequestContext().getResponse();
    downZip(zipFile,resp);,是在不行的话我就想获得uploadfiles这个文件夹绝对路径好了,请问怎么获得呢
      

  8.   

    用绝对路径然试试
    getRealPath("/")+"uploadfiles\\";
      

  9.   

    ServletActionContext.getRequest().getSession().getServletContext().getRealPath