Foi usando um MP3 player em flash, para baixar
clique aqui.
Agora temos que criar um servlet que irá mostrar a imagem, crie com o nome "JPGManager".
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.image.BufferedImage;
import java.io.*;
public class JPGManager {
static public void encodeJPG(OutputStream out, byte [] image) throws IOException{
int BUFFER = image.length;
InputStream fs = new ByteArrayInputStream(image);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(fs);
BufferedImage bImage = decoder.decodeAsBufferedImage();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bImage);
fs.close();
fs = null;
encoder = null;
bImage = null;
}
}
É preciso também criar um um servlet com o nome "pegarIMG".
A programação dele segue abaixo, e um detalhe importante é que esse servlet deve ter um parâmetro de inicialização com o nome "codigo", para que ele possa ser chamado dessa maneira: pegarIMG?codigo=1
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class pegarIMG extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
Connection con = Conectar.getConectar();
PreparedStatement ps = con.prepareStatement("SELECT FOTO FROM VOL where NUMERO = ?");
String c = request.getParameter("codigo");
ps.setString(1,c);
ResultSet rs = ps.executeQuery();
rs.next();
Blob b = rs.getBlob("FOTO");
response.setContentType("image/jpeg");
JPGManager.encodeJPG(response.getOutputStream(), b.getBytes(1, (int) b.length()) );
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pegarIMG.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pegarIMG.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
E por último, não menos importante, o servlet para resgatar o áudio, esse também deve ter um parâmetro de inicialização com o nome "codigo".
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
public class busca extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String txtbusca = request.getParameter("txtbusca");
Connection cnx = Conectar.getConectar();
String sql = "select * from VOL where numero = " +txtbusca+ "";
Statement stmt = cnx.createStatement();
ResultSet rs = stmt.executeQuery(sql);
JOptionPane.showMessageDialog(null, sql);
rs.next();
//while ( rs.next()) {
out.println("numero: " +rs.getString("numero")+ "<br>");
out.println("Foto: <img src=\" pegarIMG?codigo=" + rs.getString("numero") + "\" WIDTH=100 HEIGHT=100/> <br>");
out.println(" <object type=\"application/x-shockwave-flash\" data=\"player_mp3_mini.swf\" width=\"200\"" + "\" height=\"30\"><param name=\"movie\" value=\"player_mp3_mini.swf\" />"+ "<param name=\"bgcolor\" value=\"000000\"/> " + "<param name=\"FlashVars\" value=\"mp3=pegarSOM?codigo=" + rs.getString("numero") + "\" /> </object>" );
//}
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(busca.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(busca.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}