以下是我的代码.这样写有问题,不管我下载多少个,下载下来的文件只能得到最后一个,而且会把所有下载的文件的内容都写入
最后一个文件中,而不是连续下载多个文件
public ActionForward DownLoadFTP(ActionMapping mapping, ActionForm form, HttpServletRequest request,
         HttpServletResponse response) throws Exception
{
   ActionForward forward = new ActionForward();
   
   request.setCharacterEncoding("GBK");
   
   String[] fileName = request.getParameterValues("selectedRow");
       UserVO currUserVO = (UserVO) request.getSession().getAttribute("CurrentUser");//取当前用户
       String nativeNetId = StringHelper.convertStringNull(currUserVO.getNativeNetId());
       String ftpIp = SystemParameter.getParameterValue("IDA30_REC_FTPIP", nativeNetId);//FTPIP
       int ftpPort = Integer.parseInt(SystemParameter.getParameterValue("IDA30_REC_FTPPORT", nativeNetId));//FTP端口
       String ftpUser = SystemParameter.getParameterValue("IDA30_REC_FTPUSER", nativeNetId);//用户名
       String ftpPass = SystemParameter.getParameterValue("IDA30_REC_FTPPWD", nativeNetId);//密码
       String filePath = SystemParameter.getParameterValue("IDA30_REC_FTPPURL", nativeNetId);//下载路径
   FtpClientCommonBO ftpBO = new FtpClientCommonBO(ftpIp,ftpPort,ftpUser,ftpPass);
   try {
     int isConn = ftpBO.openFTPClient();//打开链接
     if(isConn == 1)
     {
      for(int i=0;i<fileName.length;i++)
         {
      response.reset();
      PrintWriter pw = response.getWriter();
         response.setCharacterEncoding("GBK");
         response.setContentType("application/octet-stream");
         String chineseFilename = new String(fileName[i].getBytes("GB2312"), "ISO8859_1");
         response.setHeader("Content-Disposition", "attachment; filename=" + chineseFilename);
         DataInputStream puts = ftpBO.downLoadFile(filePath + chineseFilename );
         int ch;
         while ((ch = puts.read()) >= 0) {
          pw.write(ch);
         }
         pw.close();
         pw.flush();
         puts.close();
         }
     }
} catch (Exception e) {
logger.error(e.getMessage(),e);
response.sendError(404,"文件下载出现异常! ERROR:"+e.getMessage());
}finally{
try {
ftpBO.closeFTPClient();
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}
   return null;
}

解决方案 »

  1.   

    问题可能出现在这里,DataInputStream puts = ftpBO.downLoadFile(filePath + chineseFilename ); 不知道你这个方法里面的实现,估计是文件流没有在循环里面定义成局部变量,导致每次循环取得的文件都是最后一条。
      

  2.   

    这个方法是这样的
    public DataInputStream downLoadFile(String romoteFileName) {
    DataInputStream puts = null;
    if (ftp != null) {
    try {
    Log.appLog("开始下载文件... ...");
    TelnetInputStream fget = ftp.get(romoteFileName);
     puts = new DataInputStream(fget);
    } catch (Exception ex) {
    ex.printStackTrace();
    Log.appLog("文件下载失败");
    }
    } return puts;
    }
      

  3.   

    pw.close(); 这句拿到循环外面来关。试试。