| | |
| | | |
| | | import com.java110.utils.util.Base64Convert; |
| | | import com.java110.utils.util.DateUtil; |
| | | import com.tencentcloudapi.tci.v20190318.models.FaceExpressionResult; |
| | | import org.apache.commons.net.ftp.FTP; |
| | | import org.apache.commons.net.ftp.FTPClient; |
| | | import org.apache.commons.net.ftp.FTPFile; |
| | |
| | | String userName, String userPassword, String ftpPath) { |
| | | String fileName = ""; |
| | | FTPClient ftpClient = null; |
| | | ByteArrayInputStream is = null; |
| | | try { |
| | | ftpClient = new FTPClient(); |
| | | // request.setCharacterEncoding("utf-8"); |
| | | if(!ftpClient.isConnected()){ |
| | | if (!ftpClient.isConnected()) { |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | |
| | | |
| | | |
| | | byte[] context = Base64Convert.base64ToByte(imageBase64); |
| | | ByteArrayInputStream is = new ByteArrayInputStream(context); |
| | | is = new ByteArrayInputStream(context); |
| | | boolean saveFlag = ftpClient.storeFile(fileName, is); |
| | | is.close(); |
| | | |
| | | if (!saveFlag) { |
| | | throw new IllegalArgumentException("存储文件失败"); |
| | | } |
| | |
| | | } finally { |
| | | try { |
| | | ftpClient.disconnect(); |
| | | if (is != null) { |
| | | is.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | logger.error("关闭ftpClient 失败", e); |
| | |
| | | try { |
| | | // request.setCharacterEncoding("utf-8"); |
| | | ftpClient = new FTPClient(); |
| | | if(!ftpClient.isConnected()){ |
| | | if (!ftpClient.isConnected()) { |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | |
| | | FTPClient ftpClient = null; |
| | | try { |
| | | ftpClient = new FTPClient(); |
| | | if(!ftpClient.isConnected()){ |
| | | if (!ftpClient.isConnected()) { |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | |
| | | |
| | | public static void main(String[] args) { |
| | | FtpUploadTemplate ftpUploadTemplate = new FtpUploadTemplate(); |
| | | String img = ftpUploadTemplate.download("/hc/img/20200518/","ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg", |
| | | "118.89.243.11",617,"hcdemo","45j74jpWTf7bNhnC"); |
| | | String img = ftpUploadTemplate.download("/hc/img/20200518/", "ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg", |
| | | "118.89.243.11", 617, "hcdemo", "45j74jpWTf7bNhnC"); |
| | | |
| | | System.out.printf("img="+img); |
| | | System.out.printf("img=" + img); |
| | | } |
| | | |
| | | public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) { |
| | | InputStream is = null; |
| | | ByteArrayOutputStream bos = null; |
| | | ByteArrayInputStream fis = null; |
| | | FTPClient ftpClient = new FTPClient(); |
| | | try { |
| | | if(!ftpClient.isConnected()){ |
| | | if (!ftpClient.isConnected()) { |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | |
| | | while (-1 != (length = is.read(buf, 0, buf.length))) { |
| | | bos.write(buf, 0, length); |
| | | } |
| | | ByteArrayInputStream fis = new ByteArrayInputStream( |
| | | fis = new ByteArrayInputStream( |
| | | bos.toByteArray()); |
| | | bos.flush(); |
| | | is.close(); |
| | | bos.close(); |
| | | byte[] buffer = new byte[fis.available()]; |
| | | int offset = 0; |
| | | int numRead = 0; |
| | |
| | | if (offset != buffer.length) { |
| | | throw new IOException("Could not completely read file "); |
| | | } |
| | | fis.close(); |
| | | return Base64Convert.byteToBase64(buffer); |
| | | } catch (Exception e) { |
| | | logger.error("ftp通过文件名称获取远程文件流", e); |
| | | } finally { |
| | | if (bos != null) { |
| | | try { |
| | | bos.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (is != null) { |
| | | try { |
| | | is.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if(fis != null){ |
| | | try { |
| | | fis.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | try { |
| | | if(bos != null){ |
| | | bos.close(); |
| | | } |
| | | if(is !=null) { |
| | | is.close(); |
| | | } |
| | | closeConnect(ftpClient); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |