/** 下载视频 */
@Scope(value="prototype")
@RequestMapping(value = "/filedownload.do")
public void fileDownload(String videoSrc,HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletOutputStream out = null;
FileInputStream in = null;
ByteArrayOutputStream bos = null;
ByteArrayInputStream ios = null;
try {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String videoRealPath = req.getRealPath(videoSrc);
String fileType = videoRealPath.substring(videoRealPath.lastIndexOf("."));
String videoName = "测试";
String fileName = URLDecoder.decode(videoName, "utf-8") + fileType;
File file = new File(videoRealPath);
in = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n;
while ((n = in.read(buffer)) != -1) {
bos.write(buffer, 0, n);
}
byte[] bytes = bos.toByteArray();
ios = new ByteArrayInputStream(bytes);
String _fileName = URLEncoder.encode(fileName, "UTF-8");
if (fileName.length() > 150) {
String guessCharset = "UTF-8";
_fileName = new String(fileName.getBytes(guessCharset),"ISO8859-1");
}
resp.setHeader("Content-Disposition", "attachment;filename="+ _fileName.replace("+", "%20").replaceAll("%28", "(").replaceAll("%29", ")"));
byte[] buff = new byte[1024];
out = resp.getOutputStream();
int leng = ios.read(buff);
while (leng > 0) {
out.write(buff, 0, leng);
leng = ios.read(buff);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
推荐一个公众号
号主为一线大厂架构师,CSDN博客专家,博客访问量突破一千万。主要分享Java、golang架构,源码,分布式,高并发等技术,用大厂程序员的视角来探讨技术进阶、面试指南、职业规划等。15W技术人的选择!