import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class KazMuzikServlet extends HttpServlet { private static final String filename = "kazmuzik.mp3"; public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { File file = new File(filename); int len = (int)file.length(); res.setContentType("audio/mpeg"); res.setHeader("Content-Length", "" + len); res.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); InputStream in = new BufferedInputStream(new FileInputStream(file)); OutputStream out = new BufferedOutputStream(res.getOutputStream()); byte[] b = new byte[len]; int n = 0; while (true) { int m = in.read(b, n, len-n); out.write(b, n, m); n += m; if (n == len) { break; } } in.close(); out.flush(); out.close(); return; } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doGet(req, res); } } |
Tags: programming