Read MySQL storage binary voice, picture (BLOB type)

Source: Internet
Author: User

/** * 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.