一:啓動ftp服務html
1.下載Apache FtpServer 1.0.6 Release: http://mina.apache.org/downloads-ftpserver.htmljava
2.解壓後res/conf下找到user.properties (修改密碼爲admin默認是md5加密後的)web
3.到res/conf下配置tpd-typical.xml文件spring
<server xmlns="http://mina.apache.org/ftpserver/spring/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd " id="myServer" anon-enabled="false" max-logins="100"> <listeners> <nio-listener name="default" port="21"> <ssl> <keystore file="./res/ftpserver.jks" password="password" /> </ssl> </nio-listener> </listeners> <file-user-manager file="./res/conf/users.properties" encrypt-passwords="clear"/> </server>
4.到bin目錄下:用下面命令啓動ftp服務器 ftpd.bat res/conf/ftpd-typical.xmlapache
5.打開個人電腦輸入 ftp://localhost:21/緩存
右擊登陸,輸入admin/admintomcat
二:java後臺實現上傳下載服務器
1.須要Apache Commons Net jar包引入pom.xmlapp
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency>
2.FtpUtils.classwebapp
package com.test.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.SocketException; import java.text.SimpleDateFormat; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; public class FtpUtils { private final static Logger logger = Logger.getLogger(FtpUtils.class); private final static SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd"); private final static String USER_NAME = "admin"; // ftp登陸用戶名 private final static String USER_PASSWORD = "admin"; // ftp登陸密碼 private final static String SERVER_IP = "192.168.1.133";// 直接ip地址 private final static int SERVER_PORT = 21;// 端口號 private final static String WEB_URL = "localhost:8080"; /** * 獲取ftp鏈接對象 * * @param parentDirName * @return * @throws FtpException */ public static FTPClient getFTPClient(String parentDirName) throws FtpException { FTPClient ftpClient = new FTPClient(); try { // 鏈接服務器 ftpClient.connect(SERVER_IP, SERVER_PORT); // 登錄ftp ftpClient.login(USER_NAME, USER_PASSWORD); // 判斷登錄是否成功 int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); logger.error("登錄ftp失敗"); throw new FtpException("登錄ftp失敗"); } boolean res = ftpClient.changeWorkingDirectory(parentDirName); if (!res) { ftpClient.disconnect(); logger.error("文件夾不存在"); throw new FtpException("文件夾不存在"); } // 設置屬性 ftpClient.setBufferSize(1024);// 設置上傳緩存大小 ftpClient.setControlEncoding("UTF-8");// 設置編碼 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 二進制 } catch (IOException e) { throw new FtpException("獲取ftp鏈接對象失敗", e); } return ftpClient; } /** * 關閉ftp鏈接對象 * * @param ftpClient * @throws FtpException */ public static void closeFTPClient(FTPClient ftpClient) throws FtpException { if (null != ftpClient && ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { throw new FtpException("關閉ftp鏈接對象失敗", e); } } } /** * 上傳文件 * * @param is * 輸入文件流 * @param parentDirName * 指定目錄 * @param fileName * 文件名 * @return 訪問路徑 * @throws FtpException */ public static String uploadFile(InputStream is, String parentDirName, String fileName) throws FtpException { logger.info("往:" + parentDirName + "目錄,開始上傳文件:" + fileName); String resultFileName = null; FTPClient ftpClient = getFTPClient(parentDirName); try { // 指定寫入目錄 ftpClient.storeFile(new String(fileName.getBytes("utf-8"), "iso-8859-1"), is); is.close(); resultFileName = parentDirName + File.separator + fileName; logger.info("上傳成功 :" + fileName); } catch (Exception e) { throw new FtpException("上傳文件失敗", e); } finally { closeFTPClient(ftpClient); } return WEB_URL + resultFileName; } /** * 下載文件 * * @param fileName * @param parentDirName * @param localPath * @throws FtpException */ public static void downFile(String fileName, String parentDirName, String localPath) throws FtpException { logger.info("開始從ftp服務器下載 :"); FTPClient ftpClient = getFTPClient(parentDirName); try { // 遍歷下載的目錄 FTPFile[] fs = ftpClient.listFiles(); for (FTPFile ff : fs) { // 解決中文亂碼問題,兩次解碼 byte[] bytes = ff.getName().getBytes("iso-8859-1"); String fn = new String(bytes, "utf8"); if (fn.equals(fileName)) { File localFile = new File(localPath + fileName); OutputStream is = new FileOutputStream(localFile); ftpClient.retrieveFile(fileName, is); is.close(); } } ftpClient.logout(); logger.info("從ftp服務器下載成功 :" + fileName); } catch (IOException e) { logger.error("下載 出錯:", e); throw new FtpException("下載出錯", e); } finally { closeFTPClient(ftpClient); } } /** * 從ftp服務器上下載文件到本地 * * @param sourceFileName:服務器資源文件名稱 * @return InputStream 輸入流 * @throws IOException * @throws FtpException */ public static InputStream downFile(String dirName,String sourceFileName) throws FtpException { InputStream in = null; try { in=getFTPClient(dirName).retrieveFileStream(sourceFileName); } catch (IOException e) { logger.error("下載文件出錯", e); throw new FtpException("下載文件錯誤", e); } finally { closeFTPClient(getFTPClient(dirName)); } return in; } public static void main(String[] args) throws FtpException,Exception { String dirName = "image"; String fileName = "miaotiao.jpg"; String localPathUpload = "E:\\ftp_upload\\"; String localPath = "E:\\ftp_download\\"; // InputStream is; // try { // is = new FileInputStream(new File(localPathUpload, fileName)); // String resName = FtpUtils.uploadFile(is, dirName, fileName); // System.out.println(resName); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } /**測試*/ FtpUtils.downFile(fileName, dirName, localPath); /**測試*/ // InputStream is=downFile(dirName, fileName); // File file=new File(localPath,fileName); // OutputStream os=new FileOutputStream(file); // int b; // while((b=is.read())!=-1){ // os.write(b); // } // is.close(); // os.close(); } }
FtpException.class
package com.test.ftp; public class FtpException extends Exception { /** * */ private static final long serialVersionUID = -6664802518263975091L; public FtpException(String message, Throwable cause) { super(message, cause); } public FtpException(String message) { super(message); } }
三,tomcat更改路徑映射 作圖片服務器
conf/server.xml文件
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- 加上這句 --> <Context path="upload" docBase="E://program file//apache-ftpserver-1.0.6//res//home" debug="0" reloadable="false"/> </Host>