📄 playmusic.java
字号:
/*
* DataBase Online Play Servlet by Chen Wei ying
* data:2006.7.3
*/
package com.music;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PlayMusic extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
private int len = 10 * 1024 * 1024; //定义字符数组长度,文件的最大长度
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String MusicID=request.getParameter("MusicID");
if(MusicID!=null)
{
Connection conn;
Statement st;
ResultSet rs;
InputStream in;
String SelectStr = "select * from MusicInfor where MusicID = " + MusicID;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//这里是使用数据源进行连接的,数据源的名称是DataSoure。
conn = DriverManager.getConnection("jdbc:odbc:DataSoure","sa","yeguoxing");
st = conn.createStatement();
rs = st.executeQuery(SelectStr);
rs.next();
{
//MusicData为存放音乐文件的字段,自动类型是Image
in = rs.getBinaryStream("MusicData");
//len=in.available(); 获取流的有效大小,使用时出了点问题,所以暂时屏蔽掉
//设置输出类型为音乐文件
response.setContentType("audio/mpeg");
response.reset();
OutputStream toClient = response.getOutputStream();
//FileOutputStream fos=new FileOutputStream("D:\\dddd.mp3");
byte[] P_Buf = new byte[len];
int i;
while ((i = in.read(P_Buf)) != -1)
{
toClient.write(P_Buf, 0, i);
//fos.write(P_Buf, 0, i);
}
in.close();
toClient.flush(); //强制清出缓冲区
toClient.close();
//fos.flush();
//fos.close();
}
rs.close();
conn.close();
}
catch (Exception ex)
{
System.out.print("Play Error:" + ex.toString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -