大家好,欢迎来到IT知识分享网。
下载文件Servlet
package com.what21.servlet.upload; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import sun.misc.BASE64Decoder; @WebServlet("/download/http") public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = L; private static BASE64Decoder decoder = new BASE64Decoder(); /** * 根据文件参数读取文件,然后完成下载 */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 根据参数判断文件 String filename = request.getParameter("filename"); if ((filename == null) || ("".equals(filename))) { return; } filename = new String(URLDecoder.decode(filename, "UTF-8")); File file = new File(filename); if (!file.exists()) { return; } // 制定下载格式和下载文件大小 if (file.getName().equals(".zip")) { response.setHeader("Content-type", "application/zip"); } else if (file.getName().equals(".rar")) { response.setHeader("Content-type", "application/rar"); } else { response.setContentType("application/octet-stream"); } String downloadName = URLEncoder.encode(file.getName(), "UTF-8"); response.setHeader("Content_Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "attachment; filename=" + downloadName); // 读取输入流然后输出文件 FileInputStream input = new FileInputStream(file); DataInputStream dis = new DataInputStream(input); byte[] bt = new byte[1024]; int r = -1; while ((r = dis.read(bt)) != -1) { response.getOutputStream().write(bt, 0, r); } response.getOutputStream().flush(); } }
视频流Servlet
package com.what21.servlet.upload; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; @WebServlet("/service/video") public class VideoStreamServlet extends HttpServlet { private static final long serialVersionUID = L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String diskPath = "D:/Temp/"; String diskFilename = "mda-mb3dct11xfevbuay.mp4"; response.setContentType("video/mpeg4"); byte[] data = getBytesFromFile(new File(diskPath + diskFilename)); response.setHeader("Content-Disposition", "attachment; filename=\"" + diskFilename + "\""); OutputStream os = response.getOutputStream(); os.write(data); } /** * 读取文件字节 * * @param file * @return * @throws IOException */ private static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { return null; } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while ((offset < bytes.length) && ((numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/52192.html