時間:2023-07-31 08:54:01 | 來源:網(wǎng)站運營
時間:2023-07-31 08:54:01 來源:網(wǎng)站運營
搭建Ftp文件服務器-SpringBoot工程:FTP的api文檔放在下一篇文章中/** * 獲取ftp服務器連接 * @param config 配置信息 * @return */public static FTPClient getClient(FtpConfig config) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(config.getIp(), config.getPort()); ftpClient.login(config.getUserName(), config.getPassWord()); ftpClient.enterLocalPassiveMode(); } catch (IOException e) { e.printStackTrace(); } return ftpClient;}
在服務器創(chuàng)建文件和切換到這個文件工作目錄//在服務器創(chuàng)建文件boolean makeBoolean = client.makeDirectory(loginName);boolean changeBoolen = client.changeWorkingDirectory(loginName);
文件上傳inputStream = file.getInputStream();//上傳的文件獲取流//文件重命名上傳.client.storeFile(new String(name.getBytes("UTF-8"), "UTF-8"), inputStream);
這樣ftp服務器就會創(chuàng)建一個文件夾,在文件夾中放入上傳的文件 //直接用客戶端讀取到服務器制定文件下的文件輸入流InputStream is = ftpClient.retrieveFileStream(路徑+ File.separator + 文件的名稱);//這里文件名稱和后綴都要例如:2.txt
寫入response中點擊鏈接就會下載文件:response.addHeader("Content-Disposition", "attachment;filename=" + new String((name).getBytes("gb2312"), "ISO8859-1"));ServletOutputStream out = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { length+=len; out.write(buffer, 0, len); } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } response.setHeader("Content-Length", "" + String.valueOf(length));
不會的可以參看FTP的api文檔,在下一篇文檔中關鍵詞:工程,服務,文件
微信公眾號
版權所有? 億企邦 1997-2025 保留一切法律許可權利。