| | |
| | | |
| | | 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; |
| | |
| | | try { |
| | | ftpClient = new FTPClient(); |
| | | // request.setCharacterEncoding("utf-8"); |
| | | ftpClient.connect(server, port); |
| | | if(!ftpClient.isConnected()){ |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
| | |
| | | fileName += ".jpg"; |
| | | } else if (imageBase64.contains("data:image/webp;base64,")) { |
| | | imageBase64 = imageBase64.replace("data:image/webp;base64,", ""); |
| | | fileName += ".jpg"; |
| | | } else if (imageBase64.contains("data:application/octet-stream;base64,")) { |
| | | imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", ""); |
| | | fileName += ".jpg"; |
| | | } else { |
| | | fileName += ".jpg"; |
| | |
| | | try { |
| | | // request.setCharacterEncoding("utf-8"); |
| | | ftpClient = new FTPClient(); |
| | | ftpClient.connect(server, port); |
| | | if(!ftpClient.isConnected()){ |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
| | |
| | | FTPClient ftpClient = null; |
| | | try { |
| | | ftpClient = new FTPClient(); |
| | | ftpClient.connect(server, port); |
| | | if(!ftpClient.isConnected()){ |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | if (ftpClient != null) { |
| | |
| | | FTP.DEFAULT_CONTROL_ENCODING);//防止乱码 |
| | | InputStream ins = ftpClient.retrieveFileStream(f);//需使用file.getName获值,若用f会乱码 |
| | | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
| | | byte[] buf = new byte[204800]; |
| | | byte[] buf = new byte[2048]; |
| | | int bufsize = 0; |
| | | while (ins != null && (bufsize = ins.read(buf, 0, buf.length)) != -1) { |
| | | byteOut.write(buf, 0, bufsize); |
| | | } |
| | | return_arraybyte = byteOut.toByteArray(); |
| | | //return_arraybyte = byteOut.toByteArray(); |
| | | ByteArrayInputStream fis = new ByteArrayInputStream(byteOut.toByteArray()); |
| | | byteOut.flush(); |
| | | byteOut.close(); |
| | | byte[] buffer = new byte[fis.available()]; |
| | | int offset = 0; |
| | | int numRead = 0; |
| | | while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) { |
| | | offset += numRead; |
| | | } |
| | | if (offset != buffer.length) { |
| | | throw new IOException("Could not completely read file "); |
| | | } |
| | | fis.close(); |
| | | if (ins != null) { |
| | | ins.close(); |
| | | } |
| | | return_arraybyte = buffer; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | return return_arraybyte; |
| | | } |
| | | |
| | | 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"); |
| | | |
| | | 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; |
| | | FTPClient ftpClient = new FTPClient(); |
| | | try { |
| | | if(!ftpClient.isConnected()){ |
| | | ftpClient.connect(server, port); |
| | | } |
| | | ftpClient.login(userName, userPassword); |
| | | ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); |
| | | ftpClient.enterLocalPassiveMode(); |
| | | int reply = ftpClient.getReplyCode(); |
| | | if (!FTPReply.isPositiveCompletion(reply)) { |
| | | ftpClient.disconnect(); |
| | | } |
| | | String f = new String( |
| | | (remotePath + fileName).getBytes("GBK"), |
| | | FTP.DEFAULT_CONTROL_ENCODING); |
| | | is = ftpClient.retrieveFileStream(f);// 获取远程ftp上指定文件的InputStream |
| | | if (null == is) { |
| | | throw new FileNotFoundException(remotePath); |
| | | } |
| | | bos = new ByteArrayOutputStream(); |
| | | int length; |
| | | byte[] buf = new byte[2048]; |
| | | while (-1 != (length = is.read(buf, 0, buf.length))) { |
| | | bos.write(buf, 0, length); |
| | | } |
| | | ByteArrayInputStream fis = new ByteArrayInputStream( |
| | | bos.toByteArray()); |
| | | bos.flush(); |
| | | is.close(); |
| | | bos.close(); |
| | | byte[] buffer = new byte[fis.available()]; |
| | | int offset = 0; |
| | | int numRead = 0; |
| | | while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) { |
| | | offset += numRead; |
| | | } |
| | | 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 { |
| | | try { |
| | | if(bos != null){ |
| | | bos.close(); |
| | | } |
| | | if(is !=null) { |
| | | is.close(); |
| | | } |
| | | closeConnect(ftpClient); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public void closeConnect(FTPClient ftpClient) { |
| | | try { |
| | | ftpClient.disconnect(); |