| 
 | 
 
3Java金币 
java 用ftp从linux上下载文件,下载下来提示文件损坏 
office 2007  系列的文件可以打开 如word excel ppt等 
office 2003系列的文件打不开,提示文件损坏 如word excel ppt等 
带图片的打不开 也是提示文件损坏 
linux 上是weblogic 10 
在本机下载下来都没问题 
本机是win7+tomcat6  什么原因呢? 
高手帮忙看看。 
另外会报:org.apache.commons.net.io.CopyStreamException: IOException caught while copying. 
at org.apache.commons.net.io.Util.copyStream(Util.java:129) 
        at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1452) 
        at com.lbs.brick.sys.ServiceBase.downFileFromLinux(ServiceBase.java:905) 
        at com.lbs.lrs.ls.apps.sys.fileupdate.bp.SourceBSImpl.downloadVedioFromLinux(SourceBSImpl.java:177) 
下载程序代码: 
public Boolean downFileFromLinux(String vedioName,String vedioPath,HttpServletResponse response) throws Exception{ 
                Boolean flg = false; 
                //创建ftp客户端 
                FTPClient ftpClient = new FTPClient();     
                try { 
                        if(null==vedioPath||"".equals(vedioPath)){ 
                                throw new Exception("文件路径未空"); 
                        } 
                        String hostname = IisUtil.ftpurl;//ftp访问路径 
                        int port = IisUtil.ftpport;//ftp访问端口 
                        String username = IisUtil.ftpusername;//用户名 
                        String password = IisUtil.ftppassword;  //密码         
                   //链接ftp服务器 
                   ftpClient.connect(hostname, port); 
                   ftpClient.setControlEncoding("UTF-8"); 
                        ftpClient.setDataTimeout(18000); 
                        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
                        ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); 
                        ftpClient.enterLocalPassiveMode();  
                         
                   //登录ftp 
                   ftpClient.login(username, password); 
                   int  reply = ftpClient.getReplyCode();   
                   //如果reply返回230就算成功了,如果返回530密码用户名错误或当前用户无权限下面有详细的解释。 
                   if (!FTPReply.isPositiveCompletion(reply)) {   
                            ftpClient.disconnect();  
                            throw new Exception("ftp用户名或密码错误!"); 
                   }   
                    
                   response.reset(); 
                  // response.setContentType("multipart/form-data"); 
                   response.setContentType("application/x-msdownload"); 
                        response.setCharacterEncoding("UTF-8"); 
                        response.setHeader("Content-Disposition", "attachment; filename=" 
                                        + URLEncoder.encode(vedioName, "UTF-8")); 
                         
                        OutputStream  out = response.getOutputStream(); 
                        ftpClient.retrieveFile(new String(vedioPath.getBytes("UTF-8"),"iso-8859-1"), out); 
                        out.flush(); 
                        out.close(); 
                   ftpClient.logout(); 
                   flg=true;                    
                      
                } catch (SocketException e) { 
                         
                   throw new Exception("ftp错误1:"+e.getMessage()); 
                } catch (Exception e) { 
                        e.printStackTrace(); 
                  // throw new Exception("ftp错误2:"+e.getMessage()); 
                }finally  
                {   
                   if (ftpClient.isConnected()) 
                       {   
                          try  
                            {   
                              ftpClient.disconnect();   
                            } catch (IOException ioe)  
                           {   
                              ioe.printStackTrace(); 
                           }   
                     }  
                  } 
                return flg; 
                 
        } 
 
 |   
 
 
 
 
 
 |