import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
public static void main(String[] args) throws IOException { // Read the local folder image and upload String host = "127.0.0.1"; Integer port = 21; String username = "user"; String password = "123456"; String basePath = "";
//Local picture path
String url = "C:/Users/Administrator/Desktop/Upload pictures";
File file = new File(url);
File[] files = file.listFiles();
for (File f: files) { try { // Read the picture and convert it to a stream FileInputStream fis = new FileInputStream(f); // Get the uploaded file MultipartFile mFiles = ImageUtil.base64Convert(getBase64FromInputStream(fis)); if (null != mFiles) { String saveUrls = " "; String fileName = f.getName(); String prefix = "";
String savePath = "/mingwen" + "/" + "wenxuanBook" + "/20200616";
FTPUtils.uploadFile(host, port, username, password, basePath, savePath, fileName + prefix, mFiles);
saveUrls += savePath + "/" + fileName + prefix;
// 输出流写到本地
writeUrls(saveUrls, "C:/Users/Administrator/Desktop/urls.txt");
}
} catch (Exception e) {
continue;
}
}
}
// 写入到本地
public static void writeUrls(String url, String outPath) throws IOException {
File txt = new File(outPath);
url = url + "\r\n";
byte[] bytes = new byte[512];
bytes = url.getBytes();
int length = bytes.length;
FileOutputStream fos = new FileOutputStream(txt, true);
fos.write(bytes, 0, length);
fos.flush();
fos.close();
}
public static String getBase64FromInputStream(InputStream in) { // Convert the picture file into a byte array string and perform Base64 encoding on it byte[] data = null; // Read the picture byte array try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = in.read(buff, 0, 100))> 0) { swapStream.write(buff, 0, rc) ; } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new String(Base64.encodeBase64(data));
}
package com.mingwen.common.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FTPUtils { /** * @description Upload files to FTP server * @param host FTP server hostname * @param port FTP server port * @param username FTP login account * @param password FTP login password * @param basePath FTP Server base directory * @param filePath FTP server file storage path. For example, storage by date: /2015/01/01. The path of the file is basePath+filePath * @param filename The name of the file uploaded to the FTP server * @param input input stream * @return returns true if successful, otherwise it returns false */ public static boolean uploadFile(String host, Integer port, String username , String password, String basePath, String filePath, String filename, MultipartFile file) {
boolean result = false;
FTPClient ftp = new FTPClient();
try { int reply; ftp.connect(host, port); // Connect to the FTP server // If you use the default port, you can use ftp.connect(host) directly Connect to the FTP server ftp.login(username, password);// login reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } String str = basePath + filePath ; // Switch to the upload directory if (!ftp.changeWorkingDirectory(str)) { // Create a directory if the directory does not exist
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) {
continue;
}
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
ftp.enterLocalPassiveMode();
// Set the type of uploaded file to binary type
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Upload file
if (!ftp.storeFile(filename, file.getInputStream())) { return result; } file.getInputStream().close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp. disconnect(); } catch (IOException ioe) {
}
}
}
return result;
}
/**
* @description Download files from FTP server
* @param host FTP server hostname
* @param port FTP server port
* @param username FTP login account
* @param password FTP login password
* @param remotePath relative path on FTP server
* @param fileName The name of the file to be downloaded
* @param localPath The path saved locally after downloading
* @return
*/
public static boolean downloadFile(String host, Integer port, String username, String password,
String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply;
ftp.connect(host, port);
// If you use the default port, you can use ftp.connect(host) to directly connect to the FTP server
ftp.login(username, password); // Login
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// Transfer to FTP server directory FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static boolean uploadFile(String host, Integer port, String username, String password, String basePath,
String filePath, String filename, InputStream io) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); // Connect to the FTP server // If you use the default port, you can Use ftp.connect(host) to directly connect to the FTP server ftp.login(username, password); // login reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } String str = basePath + filePath; // Switch to the upload directory if (!ftp.changeWorkingDirectory(str)) {
// If the directory does not exist, create a directory
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir: dirs) { if (null == dir || "".equals(dir) ) { continue; } tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } }
}
}
ftp.enterLocalPassiveMode();
// Set the type of uploaded file to binary type
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Upload file
if (!ftp.storeFile(filename, io)) { return result; } io. close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch ( IOException ioe) {
}
}
}
return result;
}
}