java Web保存服务器FTP上的图片到客户机

public void downloadLandMap(){
        System.out.println(realeunum);
        optResult = new JsonActionResult();
         HttpServletResponse response=ServletActionContext.getResponse();
        try{
            if(realeunum != null && realeunum.length() == 28){
                String subDir = realeunum.substring(0, 6) + "/" + 
                        realeunum.substring(6, 9) + "/" + realeunum.substring(9, 12);
                String directory = FileRootDir.LANDMAP + subDir;
                String landmappath = FileRootDir.LANDMAP + 
                realeunum.substring(0, 6) + "/" + realeunum.substring(6, 9) + "/" + 
                realeunum.substring(9, 12) + "/" + realeunum + ".jpg";
            //System.out.println(landmappath);
            InputStream is = fileCenFTPService.fileInputStreamDownload(landmappath);
             
            OutputStream ops = response.getOutputStream();
            String name=realeunum+".jpg";
            response.setContentType("image/jpeg"); 
            response.setHeader("Content-disposition", "attachment;filename="+name);
            byte[] bytes = new byte[1024];
            int iRead = 0;
            while (true) {
                iRead = is.read(bytes);
                if (iRead > 0) {
                    ops.write(bytes, 0, iRead);
                } else {
                    break;
                }
            }
            is.close();
            ops.flush();
            ops.close();

            }
            
        }catch(Exception e){
            optResult.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }finally{
            try{
                fileCenFTPService.disconnect();
            }catch (IOException er) {
                logger.error(er);
                optResult.setErrorMsg(er.getMessage());
            }
        }
    }
public InputStream fileInputStreamDownload(String remoteName) throws IOException {
        InputStream is = null;
        try {
            FTPClient ftpClient = achieveFTPClient();
            is = ftpClient.retrieveFileStream(new String(remoteName.getBytes("GBK"),"ISO-8859-1"));
        } catch (IOException er) {
            throw new IOException("下载文件流失败 : " + er.getMessage());
        }
        return is;
    }

    /***
     * 
     * @function 获取FTPClient对象
     * @return FTPClient
     * @throws Exception
     */
    private FTPClient achieveFTPClient() throws IOException{  
        if (ftpClientThreadLocal.get() != null && ftpClientThreadLocal.get().isConnected()) {  
            return ftpClientThreadLocal.get();  
        } else {  
            FTPClient ftpClient = new FTPClient(); //构造一个FtpClient实例  
//            ftpClient.setControlEncoding(encoding); //设置字符集  
      
            connect(ftpClient); //连接到ftp服务器  
      
            //设置为passive模式  
//            if (passiveMode) {  
//                ftpClient.enterLocalPassiveMode();  
//            }  
//            setFileType(ftpClient); //设置文件传输类型  
      
//            try {  
//                ftpClient.setSoTimeout(clientTimeout);  
//            } catch (SocketException e) {  
//                throw new FTPClientException("Set timeout error.", e);  
//            }
            ftpClientThreadLocal.set(ftpClient);  
            return ftpClient;  
        }  
    } 
    
    /***
     * 
     * @function ftp连接
     * @param ftpClient
     * @return  Boolean
     * @throws Exception
     */
    private boolean connect(FTPClient ftpClient) throws IOException{  
        try {  
            ftpClient.connect(url);  
  
            // 连接后检测返回码来校验连接是否成功  
            int reply = ftpClient.getReplyCode();  
  
            if (FTPReply.isPositiveCompletion(reply)) {  
                //登陆到ftp服务器  
                if (ftpClient.login(loginName, password)) {  
//                    setFileType(ftpClient);  
                    return true;  
                }  
            } else {  
                ftpClient.disconnect();  
                throw new IOException("FTP server refused connection.");  
            }  
        } catch (IOException e) {  
            if (ftpClient.isConnected()) {  
                try {  
                    ftpClient.disconnect(); //断开连接  
                } catch (IOException e1) {  
                    throw new IOException("Could not disconnect from server.", e1);  
                }  
  
            }  
            throw new IOException("Could not connect to server.", e);  
        }  
        return false;  
    }  
    
    /***
     * @function 断开连接
     * 
     * @throws IOException
     */
    public void disconnect() throws IOException {  
        try {  
            FTPClient ftpClient = achieveFTPClient(); 
            if (ftpClient.isConnected()) {  
                ftpClient.disconnect();  
                ftpClient = null;  
            }  
        } catch (IOException e) {  
            throw new IOException("Could not disconnect from server.", e);  
        }  
    }  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容