In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "FTP server package download file steps", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "FTP server package download file steps" bar!
Requirements: download files from ftp server package
Solution steps: 1. Download each file from the ftp server to the local server (usually the server where tomcat is installed, and the local server where the project's own computer runs is your own computer).
two。 Package the downloaded folder in the local server and return the packaged File zip
3.zip files are written to reponse by stream to achieve the download effect of users.
Prepare the file:
/ / encapsulate all the file addresses that need to be packaged and downloaded request class public class DownloadPackageReq implements Serializable {/ / local server temporarily stores directory name (.eg: "menutree20200904112125" as far as possible) private String localTempDirName; / / packaged download local server folder name private List downloadPackageListReqList; / / all file paths and names need to be downloaded} / / FTPClientUtils:ftp tool class 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 } / * download a remote file to the specified local file * * @ param remoteAbsoluteFile * remote file name (including full path, eg:/MTL/test/menutree_attachment/file.xlsx) * @ param localAbsoluteFile * local file name (including full path) * @ param autoClose * whether to automatically close the current connection * * @ return, return true Return false * @ throws Exception * / public boolean get (String remoteAbsoluteFile, String localAbsoluteFile, boolean autoClose) throws Exception {OutputStream output = null if failed 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) } / * download a remote file to the specified stream and remember to close the stream * * @ param remoteAbsoluteFile * @ param output * @ param autoClose * @ return * @ throws Exception * / public boolean get (String remoteAbsoluteFile, OutputStream output, boolean autoClose) throws Exception {try {FTPClient ftpClient = getFTPClient (); / / process transfer return ftpClient.retrieveFile (remoteAbsoluteFile, output) } catch (IOException e) {throw new Exception ("Couldn't get file from server.", e);} finally {if (autoClose) {disconnect (); / / close link}
Step one:
Public File downloadMenuTreeAttachment (Integer menutreeId) throws Exception {/ / get all the file addresses corresponding to menutreeId List resourcesMenutreeLists = resourcesMenutreeListMapper.selExistingAttachment (menutreeId); DownloadPackageReq req = new DownloadPackageReq (); if (CollectionUtils.isNotEmpty (resourcesMenutreeLists)) {req.setLocalTempDirName (resourcesMenutreeLists.get (0). GetMenutreeName () + DateUtils.dateTimeNow ()); List dpList = new ArrayList (); for (ResourcesMenutreeListVo temp: resourcesMenutreeLists) {DownloadPackageListReq dpReq = new DownloadPackageListReq () / file name, used to download ftp server file modification file name (because ftp files are all uuid names) String fileName = temp.getModuleName () + "_" + temp.getEngineerName (); dpReq.setFileName (fileName); dpReq.setFileFtpUrl (temp.getMenutreeAttachment ()); dpList.add (dpReq);} req.setDownloadPackageListReqList (dpList);} else {req.setLocalTempDirName ("empty menu tree" + DateUtils.dateTimeNow ());} return ftpService.zipFiles (req) } public File zipFiles (DownloadPackageReq req) throws Exception {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()). GetRequest (); / / Local server staging path String localTempDir = request.getSession (). GetServletContext (). GetRealPath ("/") + req.getLocalTempDirName () + File.separator;logger.info ("local server staging path:" + localTempDir); File dir = new File (localTempDir); if (! Dir.exists () {dir.mkdir ();} if (CollectionUtils.isNotEmpty (req.getDownloadPackageListReqList () {List downloadList = req.getDownloadPackageListReqList (); FTPClientUtils ftp = FTPClientUtils.init (); for (int iTuno; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.