/** * Download Voice * Remarks: * @throws Exception */
public void Downloadyuyin () throws Exception {
String Voiceid = Getrequest (). GetParameter ("Voiceid");
InputStream InputStream = Voiceservice.findinputbyvoiceid (Voiceid);
String realPath1 = Getrequest (). Getrealpath ("/") + "/upload/voice.pttly";
File F2 = new file (realPath1); FileOutputStream dos = new FileOutputStream (F2); Generating a binary write stream with buffers
OutputStream dosss = new DataOutputStream (new Bufferedoutputstream (New FileOutputStream (F2)));
Create a file read buffer
byte[] buf = new byte[2048];
Read into buffer Dos.write (Tobytearray (InputStream));
Write file data to file
Dos.close ();//Close stream
String Realpath = Getrequest (). Getrealpath ("/") + "/upload/voice.pttly";
File File = new file (Realpath);
String filename = File.getname ();
InputStream fis = new Bufferedinputstream (new FileInputStream (Realpath));
byte[] buffer = new byte[fis.available ()]; Fis.read (buffer); Fis.close (); Empty
Response GetResponse (). reset ();
Set the header of the response
GetResponse (). AddHeader ("Content-disposition", "attachment;
Filename= "+ New String (Filename.getbytes ()));
GetResponse (). AddHeader ("Content-disposition", "attachment;filename=" + "yuyin.pttly");
GetResponse (). AddHeader ("Content-length", "" "+ File.length ());
OutputStream toclient = new Bufferedoutputstream (GetResponse (). Getoutputstream ());
GetResponse (). setContentType ("Application/octet-stream");
Toclient.write (buffer);
Toclient.flush ();
Toclient.close ();
}
Service
Public InputStream Findinputbyvoiceid (String voiceid) {
Return Voicedao.findinputbyvoiceid (Voiceid);
}
Dao
@SuppressWarnings ("Rawtypes")
public inputstream Findinputbyvoiceid (String voiceid) {
String sql = "Select Voice_id,call_user_id,group_id,unitid,participate_user_id,call_type,starttime,endtime, VoiceData from Voice_info WHERE voice_id= "+voiceid;
sqlquery sqlquery = Createsqlquery (sql.tostring ());
list lists = Sqlquery.list ();
inputstream input = null;
for (int i=0;i<lists.size (); i++) {
object[] objects= (object[]) lists.get (0) ;
if (Objects! = null) {
serializableblob o = (serializableblob) OBJECTS[8];
try {
input = O.getbinarystream ();
} catch (SQLException e) {
e.printstacktrace ();
}
}
}
return input;
}
Read MySQL storage binary voice, picture (BLOB type)