Browser Preview Remote FTP pictures

Source: Internet
Author: User
Tags base64 ftp int size

Requirement: Preview a picture on a Web page with a password on the FTP server

Recently encountered a need to display the FTP side of the picture on the page, but do not download to local. Idea one: from the background to the picture path, directly in the IMG src in the FTP picture path, found not to display, Baidu after discovering the need to also pass in the FTP user name and password, write as follows:

Tried a bit, wow, show out, so easy, but in chrome access, found that the picture did not appear, the console error, click the link is as follows:



This means that the same page from the 59 version of Chrome prohibits reading resources from different sources, that is, HTTP pages can not embed FTP resources. Try the chrome54 version is available to display.
Thought two: No way, arm can not twist thigh, carry but Google, only find another way, think from the server to get pictures, read out, turn into Base64 format to the foreground,
First Baidu find Java from FTP to get the code of the file, online much, and then find the image into the BASE64 format code, is a search a lot:
Import org.apache.commons.net.ftp.FTPClient; Import org.apache.commons.codec.binary.Base64; Import Org.apache.commons.net.ftp.FTPFile;
FtpClient FC = new FtpClient ();
Connect the FTP fc.connect ("test.3mkj.com", 21);
Login to Fc.login ("Bill", "BillSanMa1030");
Set the encoding format fc.setcontrolencoding ("UTF-8");
String remoteabsolutefile = Fs[0].getname (); Remoteabsolutefile = new String (remoteabsolutefile.getbytes ("UTF-8"), "iso-8859-1");
Get input stream InputStream in = Fc.retrievefilestream (Remoteabsolutefile); byte[] bytes = new byte[in.available ()]; int length = in.read (bytes);
String base64image = new String (base64.encodebase64 (bytes));

However, the validation found that it did return a string in Base64 format, but it is not the correct picture conversion, the Web can not find more detailed information, after careful study found that in.available () smaller than the actual file, view FtpClient The description of API Discovery Retrievefilestream is "returns the InputStream from which a specified file can be read from the server. "In other words, it is possible to return a inputstream that is not a complete file, continue to view the API, and find Retrievefile (String remote, OutputStream local) ( Retrieves the named file from the server and writes it to the given outputstream. ), try to use the Output Stream, and then modify the code to:
ftpfile[] fs = Fc.listfiles ();
Get File size
int size = (int) fs[0].getsize ();
byte[] bytes = new Byte[size];
Bytearrayoutputstream OS = new Bytearrayoutputstream ();
Write output stream
Fc.retrievefile (Remoteabsolutefile,os);
bytes = Os.tobytearray ();
String base64image = new String (base64.encodebase64 (bytes));

Problem Solving ~ above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.