//播放视频
@RequestMapping("/getVideo")
public void ss(HttpServletRequest request, HttpServletResponse response, String fileName, String fileLength, String type, String fileFormat) throws IOException {
OutputStream os = null;
FTPClient ftpClient = ftpFileUtil.getFTPClient();
try {
os = response.getOutputStream();
String rangeString = request.getHeader("Range");//如果是video标签发起的请求就不会为null
long range = Long.valueOf(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
response.setHeader("Content-Type", type + "/" + fileFormat);
response.setContentLength(Integer.parseInt(fileLength));//10000是视频文件的大小,上传文件时都会有这些参数的
response.setHeader("Content-Range", String.valueOf(range + (Integer.parseInt(fileLength) - 1)));//拖动进度条时的断点,其中10000是上面的视频文件大小,改成你的就好
response.setHeader("Accept-Ranges", "bytes");
int index = fileName.indexOf("/");
String path = fileName.substring(0, fileName.indexOf("/", index + 1));
String name = fileName.substring(path.length() + 1, fileName.length());
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
ftpClient.changeWorkingDirectory(path);
ftpClient.retrieveFile(name, os);
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
os.flush();
os.close();
if (ftpClient.isConnected()) {
try {
ftpClient.logout();
} catch (IOException e) {
}
}
}
}
//图片转base64在线预览
@PostMapping("/redimagebase")
public AjaxResult redimagebase(String localName) {
InputStream inputStream = null;
FTPClient ftpClient = null;
String base = "data:image/jpeg;base64,";
String re = null;
try {
int index = localName.indexOf("/");
String path = localName.substring(0, localName.indexOf("/", index + 1));
String name = localName.substring(path.length() + 1, localName.length());
ftpClient = ftpFileUtil.getFTPClient();
ftpClient.changeWorkingDirectory(path);
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
byte[] bytes = ff.getName().getBytes("iso-8859-1");
String fileName = new String(bytes, "utf-8");
if (name.equals(fileName)) {
inputStream = ftpClient.retrieveFileStream(fileName);
}
}
String s = Base64Util.inputStream2Base64(inputStream);
re=s;
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String base64 = base + re;
return AjaxResult.success(base64);
}
//util
package com.csyk.utils;
import sun.misc.BASE64Decoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class Base64Util {
public static String inputStream2Base64(InputStream is) throws Exception {
byte[] data = null;
try {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = is.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
data = swapStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
throw new Exception("输入流关闭异常");
}
}
}
return Base64.getEncoder().encodeToString(data);
}
private static InputStream base2InputStream(String base64string) {
ByteArrayInputStream stream = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64string);
stream = new ByteArrayInputStream(bytes1);
} catch (Exception e) {
e.printStackTrace();
}
return stream;
}
}
在线播放ftp上视频,ftp图片转base在线预览
于 2022-11-01 11:03:58 首次发布