FtpWebRequest reqFTP;
string ftpUrl = "ftp://a.png";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("123", "123456");
reqFTP.UsePassive = false;
reqFTP.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
MemoryStream mStream = new MemoryStream();
ftpStream.CopyTo(mStream);
byte[] arr = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(arr, 0, (int)mStream.Length);
byte[] bytes = mStream.GetBuffer();
string base64 = Convert.ToBase64String(bytes);
mStream.Close();
return base64;
得到的base64图片,在前端显示不出来
Java将二进制流转Base64字符串并在页面显示(附Base64转二进制流)
java代码如下:
/**
* 二进制流转Base64字符串
*
* @param data 二进制流
* @return data
* @throws IOException 异常
*/
public static String getImageString(byte[] data) throws IOException {
BASE64Encoder encoder = new BASE64Encoder();
return data != null ? encoder.encode(data) : "";
}
/**
* Base64字符串转 二进制流
*
* @param base64String Base64
* @return base64String
* @throws IOException 异常
*/
public static byte[] getStringImage(String base64String) throws IOException {
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
return base64String != null ? decoder.decodeBuffer(base64String) : null;
}
前台页面代码如下:
前台用的是freemarker模板引擎
效果如图:
FtpWebRequest reqFTP;
string ftpUrl = "ftp://a.png";
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("123", "123456");
reqFTP.UsePassive = false;
reqFTP.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
MemoryStream mStream = new MemoryStream();
ftpStream.CopyTo(mStream);
byte[] arr = new byte[mStream.Length];
mStream.Position = 0;
mStream.Read(arr, 0, (int)mStream.Length);
byte[] bytes = mStream.GetBuffer();
string base64 = Convert.ToBase64String(bytes);
mStream.Close();
return base64;
得到的base64图片,在前端显示不出来