This article shares the specific code for Java to upload images to the server function for your reference. The specific content is as follows
This case realizes the image upload function in two steps, respectively
(1) The APP uses base64 encryption to upload the image content to the server (http protocol), and store the pictures first in the temporary directory;
(2) Upload the images temporarily stored by the server using the FTP protocol to another server specially used for storing pictures;
/** * ftp file operation service implementation class* */@Servicepublic class FtpFileServiceImpl implements IFtpFileService { /**ftp server*/ @Value("${PTFSERVER}") private String server; /**Ftp username*/ @Value("${PTFUSERNAME}") private String uname; /**ftp password*/ @Value("${PTFPWD}") private String pwd; /** local character set encoding*/ private static final String LOCAL_CHARSET = "GBK"; /**ftp Server character set encoding*/ private static final String SERVER_CHARSET = "ISO-8859-1"; /** * ftp file upload*/ @Override public void ftpUpload(File srcFile, String fileName, String foldName) { FTPClient ftpClient = new FTPClient(); FileInputStream fis = null; String charset = LOCAL_CHARSET; try { ftpClient.connect(server); ftpClient.login(uname, pwd); fis = new FileInputStream(srcFile); // Set the upload directory ftpClient.changeWorkingDirectory(foldName); ftpClient.setBufferSize(1024); ftpClient.enterLocalPassiveMode(); if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) { // Turn on the server's support for UTF-8, if the server supports it, use UTF-8 encoding, otherwise use local encoding (GBK). charset = "UTF-8"; } ftpClient.setControlEncoding(charset); fileName = new String(fileName.getBytes(charset),SERVER_CHARSET); // Set file type (binary) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.storeFile(fileName, fis); } catch (IOException e) { throw new EhospitalServiceException(ResponseCode.RESPONSE_COMMON_ERROR_CODE, "The FTP client error!", e); } Finally { try { ftpClient.disconnect(); } catch (IOException e) { throw new EhospitalServiceException(ResponseCode.RESPONSE_COMMON_ERROR_CODE, "Exception occurred when closing the FTP connection!", e); } } } } 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.