A few days ago, I did a project. The client and the administrator were written together, and they shared the same server. The files uploaded by the client were all on the server's hard disk. Lao Long proposed to separate the client and the administrator side, and at this time there was a problem with the storage of the attachments uploaded by the user. Obviously, it is unrealistic to store apk files that are as large as hundreds of M to the database. After checking for a long time, it is the fastest way to establish an ftp server to transmit files on both ends.
The specific process is that the user logs into the external network client and uploads the file to the external network server hard disk. At the same time, the file accesses the ftp server of the intranet administrator server through the external network server and passes it to the intranet server hard disk. In this way, the intranet server can encrypt and sign the uploaded files, and then pass the files back to the external network server hard disk through ftp for users to perform other operations.
The tools used in the specific implementation: Serv-U. Serv-U is a tool that facilitates us to establish an ftp server on Windows. After downloading and cracking, follow the steps, set the IP, port, account password, disk path that allows ftp access, operation permissions, etc., and you can use it. When testing the IP, choose 127.0.0.1, and when testing the IP in the intranet, choose 192.168.0.x.
In the implementation of the Java project, I wrote a tool class myself and used the commons-net package of apache, which has upload, download and delete functions. Attached code:
package app.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 org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * FTP server tool class* */ public class FTPUtils { /** * Upload file to FTP server* * @param url * Server IP address* @param port * Server port* @param userName * User login name* @param password * User login password* @param storePath * Server file storage path* @param fileName * Server file storage name* @param is * File input stream* @return * <b>true</b>: Upload successfully* <br/> * <b>false</b>: Upload failed*/ public static boolean storeFile (String url, int port, String userName, String password, String storePath, String fileName, InputStream is) { boolean result = false; FTPClient ftp = new FTPClient(); try { // When connecting to the server, the port defaults to 21, you can directly connect ftp.connect(url ,port); // Log in to the server ftp.login(userName, password); // Determine whether the return code is legal if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // Disconnect when illegal ftp.disconnect(); // End the program return result; } // Determine whether the ftp directory exists. If it does not exist, create a directory, including creating a multi-level directory String s = "/"+storePath; String[] dirs = s.split("/"); ftp.changeWorkingDirectory("/"); // Check whether the directory exists in order. If it does not exist, create a directory for(int i=1; dirs!=null&&i<dirs.length; i++) { if(!ftp.changeWorkingDirectory(dirs[i])) { if(ftp.makeDirectory(dirs[i])) { if(!ftp.changeWorkingDirectory(dirs[i])) { return false; } }else { return false; } } } // Set file operation directory ftp.changeWorkingDirectory(storePath); // Set file type, binary ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Set buffer size ftp.setBufferSize(3072); // Upload file result = ftp.storeFile(fileName, is); // Close the input stream is.close(); // Logout the server ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // Check if (null != is) { // Close the input stream is.close(); } // Check if the connection exists if (ftp.isConnected()) { // Disconnect ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * Download file from the FTP server to local* * @param url * Server IP address * @param port * Server port * @param userName * User login name * @param password * User login password * @param remotePath * Server file storage path * @param fileName * Server file storage name * @param localPath * Local file storage path * @return * <b>true</b>: Download successfully* <br/> * <b>false</b>: Download failed*/ public static boolean retrieveFile (String url, int port, String userName, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // Disconnect when the port is not legal; // End the program return result; } // Set the file operation directory ftp.changeWorkingDirectory(remotePath); // Set the file type, binary ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Set the buffer size ftp.setBufferSize(3072); // Set the character encoding ftp.setControlEncoding("UTF-8"); // Construct the local file object File localFile = new File(localPath + "/" + fileName); // Get all file names in the file operation directory String[] remoteNames = ftp.listNames(); // Looping the file name to determine whether it contains the file name to be downloaded for (String remoteName: remoteNames) { if (fileName.equals(remoteName)) { result = true; } } // When the file name comparison is successful, enter the download process if (result) { // Construct the file output stream os = new FileOutputStream(localFile); // Download the file result = ftp.retrieveFile(fileName, os); // Close the output stream os.close(); } // Log out the server ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // Determine if the output stream exists if (null != os) { // Close the output stream os.close(); } // Determine if the connection exists if (ftp.isConnected()) { // Disconnect ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * Delete file from the FTP server* * @param url * Server IP address* @param port* @param userName * User login name* @param password * User login password* @param remotePath * Server file storage path* @param fileName * Server file storage name* @return * <b>true</b>: Deletion was successful* <br/> * <b>false</b>: Deletion failed*/ public static boolean deleteFile (String url, int port, String userName, String password, String remotePath, String fileName) { boolean result = false; FTPClient ftp = new FTPClient(); try { // When connecting to the server, the port defaults to 21, you can directly connect ftp.connect(url ,port); // Log in to the server ftp.login(userName, password); // Determine whether the return code is legal if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { // Disconnect when it is not legal ftp.disconnect(); // End the program return result; } // Set the file operation directory ftp.changeWorkingDirectory(remotePath); // Set the file type, binary ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Set the buffer size ftp.setBufferSize(3072); // Set the character encoding ftp.setControlEncoding("UTF-8"); // Get all file names in the file operation directory String[] remoteNames = ftp.listNames(); // Loop to compare the file names to determine whether the file name to be downloaded is currently contained for (String remoteName: remoteNames) { if (fileName.equals(remoteName)) { result = true; } } // When the file name comparison is successful, enter the deletion process if (result) { // Delete file result = ftp.deleteFile(fileName); } // Log out of the server ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { // Determine if the connection exists if (ftp.isConnected()) { // Disconnect ftp.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return result; } public static void main(String[] args) throws FileNotFoundException { // try { // FileInputStream fis = new FileInputStream(new File("D:/Soft Storage/Soft Toolbox/HTML_Help_WorkShop_1.3_XiaZaiBa.zip")); // System.out.println(storeFile("192.168.1.2", 21, "admin", "1", "C:/Documents and Settings/Administrator/Desktop", RandomUUID.random() + ".zip", fis)); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // //File file = new File("C:/Users/freed/Desktop/1.txt"); //InputStream is = new FileInputStream(file); //System.out.println(storeFile("127.0.0.1", 21, "feili", "feili", "examples", "2.txt", is)); //System.out.println(retrieveFile("127.0.0.1", 21, "feili", "feili", "examples/jsp", "index.html", "C:/Users/freed/Desktop")); //System.out.println(deleteFile("127.0.0.1", 21, "feili", "feili", "testpath", "1.txt")); } }It should be noted that when uploading files, you must first put the File file into the fileinputstream.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.