溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

FTP服務(wù)器打包下載文件的步驟

發(fā)布時間:2021-09-04 18:26:24 來源:億速云 閱讀:333 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要講解了“FTP服務(wù)器打包下載文件的步驟”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“FTP服務(wù)器打包下載文件的步驟”吧!

需求:從ftp服務(wù)器打包下載文件

解決步驟:1.從ftp服務(wù)器把各個文件下載到本地服務(wù)器(一般是安裝tomcat的服務(wù)器,項(xiàng)目自己電腦跑的本地服務(wù)器就是自己電腦)指定目錄中

2.在本地服務(wù)器打包下載好的文件夾打包,返回打包好的File zip

3.zip文件用流寫入reponse,達(dá)到用戶下載效果

準(zhǔn)備文件:

// 封裝所有需要打包下載的文件地址請求類
public class DownloadPackageReq implements Serializable {// 本地服務(wù)器臨時存放目錄名(盡量唯一.eg:"menutree20200904112125")    private String localTempDirName; // 打包下載本地服務(wù)器文件夾名字private List<DownloadPackageListReq> downloadPackageListReqList; // 需要下載所有文件路徑和名稱
}
// FTPClientUtils:ftp工具類
public static FTPClientUtils init() {   FTPClientUtils ftp = new FTPClientUtils();   ftp.setHost(host);   ftp.setPort(port);   ftp.setUsername(username);   ftp.setPassword(password);   ftp.setBinaryTransfer(true);   ftp.setPassiveMode(false);   ftp.setEncoding("utf-8");   return ftp;
}
/** * 下載一個遠(yuǎn)程文件到本地的指定文件 * 
 * @param remoteAbsoluteFile *            遠(yuǎn)程文件名(包括完整路徑,eg:/MTL/test/menutree_attachment/file.xlsx) * @param localAbsoluteFile *            本地文件名(包括完整路徑) * @param autoClose *            是否自動關(guān)閉當(dāng)前連接 * 
 * @return 成功時,返回true,失敗返回false * @throws Exception */public boolean get(String remoteAbsoluteFile, String localAbsoluteFile, boolean autoClose) throws Exception {   OutputStream output = null;   try {
      output = new FileOutputStream(localAbsoluteFile);      return get(remoteAbsoluteFile, output, autoClose);
   } catch (FileNotFoundException e) {      throw new Exception("local file not found.", e);
   } finally {      try {         if (output != null) {
            output.close();
         }
      } catch (IOException e) {         throw new Exception("Couldn't close FileOutputStream.", e);
      }
   }
}
/** * 下載一個遠(yuǎn)程文件到指定的流 處理完后記得關(guān)閉流 * 
 * @param remoteAbsoluteFile * @param output * @param autoClose * @return * @throws Exception */public boolean get(String remoteAbsoluteFile, OutputStream output, boolean autoClose) throws Exception {   try {      FTPClient ftpClient = getFTPClient();      // 處理傳輸      return ftpClient.retrieveFile(remoteAbsoluteFile, output);
   } catch (IOException e) {      throw new Exception("Couldn't get file from server.", e);
   } finally {      if (autoClose) {
         disconnect(); // 關(guān)閉鏈接      }
   }
}

第一步:

public File downloadMenuTreeAttachment(Integer menutreeId) throws Exception {
    // 從數(shù)據(jù)庫拿到menutreeId對應(yīng)的所有文件地址List<ResourcesMenutreeListVo> resourcesMenutreeLists = resourcesMenutreeListMapper.selExistingAttachment(menutreeId);DownloadPackageReq req = new DownloadPackageReq();if (CollectionUtils.isNotEmpty(resourcesMenutreeLists)) {req.setLocalTempDirName(resourcesMenutreeLists.get(0).getMenutreeName() + DateUtils.dateTimeNow());List<DownloadPackageListReq> dpList = new ArrayList<>();for(ResourcesMenutreeListVo temp : resourcesMenutreeLists) {DownloadPackageListReq dpReq = new DownloadPackageListReq();
            // 文件名稱,用來下載ftp服務(wù)器文件修改文件名(因?yàn)閒tp文件都是uuid名稱)String fileName = temp.getModuleName() + "_" + temp.getEngineerName();dpReq.setFileName(fileName);dpReq.setFileFtpUrl(temp.getMenutreeAttachment());dpList.add(dpReq);
        }req.setDownloadPackageListReqList(dpList);
    } else {req.setLocalTempDirName("空菜單樹" + DateUtils.dateTimeNow());
    }return ftpService.zipFiles(req);
}
public File zipFiles(DownloadPackageReq req) throws Exception {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();// 本地服務(wù)器暫存路徑    String localTempDir = request.getSession().getServletContext().getRealPath("/") + req.getLocalTempDirName() + File.separator;logger.info("本地服務(wù)器暫存路徑:" + localTempDir);File dir = new File(localTempDir);if ( ! dir.exists()) {dir.mkdir();
    }if (CollectionUtils.isNotEmpty(req.getDownloadPackageListReqList())) {List<DownloadPackageListReq> downloadList = req.getDownloadPackageListReqList();FTPClientUtils ftp = FTPClientUtils.init();for(int i=0; i<downloadList.size(); i++) {// 是否關(guān)閉傳輸流(最后一份文件傳輸完畢關(guān)閉)            boolean isCloseStream = false;if (i == downloadList.size() - 1) {
                isCloseStream = true;
            }DownloadPackageListReq temp = downloadList.get(i);String fileFtpUrl = temp.getFileFtpUrl();String suffix = "";if (fileFtpUrl.contains(".") && !fileFtpUrl.endsWith(".")) {// 文件后綴名                suffix = fileFtpUrl.substring(fileFtpUrl.lastIndexOf("."));
            }// 本地服務(wù)器存放完整路徑(包含文件名)            String localUrl = localTempDir + temp.getFileName() + suffix;// 下載的第一個參數(shù)遠(yuǎn)程路徑如果是FTP服務(wù)器,就采用服務(wù)器完整文件路徑(eg:/MTL/test/menutree_attachment/file.xlsx),因?yàn)槌跏蓟痜tp服務(wù)器就帶了ip端口            boolean result = ftp.get(temp.getFileFtpUrl(), localUrl, isCloseStream);
        }
    }
    // 打包下載好的文件File zipFile = ZipUtil.zip(localTempDir, localTempDir + req.getLocalTempDirName() + ".zip");return zipFile;
}

第二步:

public class ZipUtil {private static Logger logger = Logger.getLogger(ZipUtil.class);/**     * 緩沖器大小     */    private static final int BUFFER = 512;/**     * 壓縮方法 (可以壓縮空的子目錄)     *     * @param srcPath     壓縮源路徑     * @param zipFileName 目標(biāo)壓縮文件     * @return     */    public static File zip(String srcPath, String zipFileName) {ZipOutputStream zipOutputStream = null;InputStream inputStream = null;File outputZipFile = null;try {// 檢查文件是否存在,是的話先刪除            outputZipFile = new File(zipFileName);if (outputZipFile.exists()) {
                outputZipFile.delete();
            }File srcFile = new File(srcPath);List<File> fileList = FileUtil.getAllFiles(srcFile);// 所有要壓縮的文件            byte[] buffer = new byte[BUFFER];// 緩沖器            ZipEntry zipEntry = null;int readLength = 0;// 每次讀出來的長度            zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileName));for (File file : fileList) {if (file.isFile()) {// 若是文件,則壓縮這個文件                    zipEntry = new ZipEntry(getRelativePath(srcPath, file));
                    zipEntry.setSize(file.length());
                    zipEntry.setTime(file.lastModified());
                    zipOutputStream.putNextEntry(zipEntry);

                    inputStream = new BufferedInputStream(new FileInputStream(file));while ((readLength = inputStream.read(buffer, 0, BUFFER)) != -1) {
                        zipOutputStream.write(buffer, 0, readLength);
                    }
                } else {// 若是目錄(即空目錄)則將這個目錄寫入zip條目                    zipEntry = new ZipEntry(getRelativePath(srcPath, file) + File.separator);
                    zipOutputStream.putNextEntry(zipEntry);
                }
            } // end for        } catch (FileNotFoundException e) {logger.error("zip fail!", e);
        } catch (IOException e) {logger.error("zip fail!", e);
        } finally {close(inputStream);close(zipOutputStream);
        }// 返回文件輸出流        outputZipFile = new File(zipFileName);return outputZipFile;
    }/**     * 關(guān)閉流     */    private static void close(Closeable c) {if (c == null)return;try {
            c.close();
        } catch (IOException e) {logger.error("close fail!", e);
        }
        c = null;
    }/**     * 取相對路徑 依據(jù)文件名和壓縮源路徑得到文件在壓縮源路徑下的相對路徑     *     * @param dirPath 壓縮源路徑     * @param file     * @return 相對路徑     */    public static String getRelativePath(String dirPath, File file) {File dir = new File(dirPath);String relativePath = file.getName();while (true) {
            file = file.getParentFile();if (file == null) {break;
            }if (file.equals(dir)) {break;
            } else {
                relativePath = file.getName() + "/" + relativePath;
            }
        } // end while        return relativePath;
    }
}

第三步:Controller控制器,Result是自己封裝的返回類,可以自定義String之類的返回

public Result downloadMenuTreeAttachment(Integer menutreeId, HttpServletResponse response) {BufferedInputStream bis = null;OutputStream os = null;try {File file = resourcesMenutreeListService.downloadMenuTreeAttachment(menutreeId);
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentLength((int) file.length());// 設(shè)置content-disposition響應(yīng)頭控制瀏覽器以下載的形式打開文件,中文文件名要使用URLEncoder.encode方法進(jìn)行編碼,否則會出現(xiàn)文件名亂碼        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        bis = new BufferedInputStream(new FileInputStream(file));
        os = response.getOutputStream();byte[] buff = new byte[1024];int i = 0;while ((i = bis.read(buff)) != -1) {
            os.write(buff, 0, i);
            os.flush();
        }
    } catch (Exception e) {log.error("{}",e);return ResultGenerator.genFailResult("下載失敗");
    } finally {try {
            bis.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }return ResultGenerator.genSuccessResult();
}

感謝各位的閱讀,以上就是“FTP服務(wù)器打包下載文件的步驟”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對FTP服務(wù)器打包下載文件的步驟這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI