protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
InputStream inputStream = null; // input stream of the upload file

// obtains the upload file part in this multipart request
Part filePart = request.getPart("audio");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());

// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}

String message = null; // message will be sent back to client

try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

// Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, "root", "root");

// constructs SQL statement
String sql = "INSERT INTO user_music_library (id,audio) values (?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1,"1");

if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(2, inputStream);
}

// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
}
// catch(Exception e)
// {
// pw.println(e); 
// }
catch(SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
}
// sets the message in request scope
request.setAttribute("Message", message);

// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}

this is my code to upload audio bt cannot upload only upto amount of kbs. i used BLOB datatype in mysql it does not have anylongblob.
so now can anyone help me upload audio,display them on jsp and play them after selecting them

Dani AI

Generated

the “only up to KB” cap almost always means the column type is too small. BLOB tops out around 64 KB, which matches your symptom. Use MEDIUMBLOB (<= 16 MB) or LONGBLOB (<= ~4 GB) instead, and also store the MIME type so you can return the correct Content-Type when streaming back to the browser.

SQL:

ALTER TABLE user_music_library 
  MODIFY audio LONGBLOB NOT NULL,
  ADD COLUMN mime_type VARCHAR(100) NULL;

See MySQL’s limits, and note you may also hit servlet/container limits: set @MultipartConfig(maxFileSize=..., maxRequestSize=...) on your upload servlet, and raise Tomcat’s Connector maxPostSize if needed (defaults to 2 MiB). MySQL BLOB/TEXT limits. @MultipartConfig docs. Tomcat HTTP Connector maxPostSize. (dev.mysql.com)

To play audio, do not stream bytes from a JSP. Point an HTML5 audio tag at a download servlet, as hinted:

<audio controls src="${pageContext.request.contextPath}/audio?id=1"></audio>

And stream from the DB without loading the whole blob into memory:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
  try (Connection c = ds.getConnection();
       PreparedStatement ps = c.prepareStatement(
         "SELECT mime_type, OCTET_LENGTH(audio), audio FROM user_music_library WHERE id=?")) {
    ps.setString(1, req.getParameter("id"));
    try (ResultSet rs = ps.executeQuery()) {
      if (!rs.next()) { resp.sendError(404); return; }
      resp.setContentType(rs.getString(1));
      resp.setHeader("Accept-Ranges", "bytes");
      try (InputStream in = rs.getBinaryStream(3); OutputStream out = resp.getOutputStream()) {
        byte[] buf = new byte[8192]; for (int n; (n = in.read(buf)) > 0; ) out.write(buf, 0, n);
      }
    }
  }
}

If uploads still fail, increase MySQL’s max_allowed_packet on both client and server. The server default is 64 MB; exceeding it triggers “Packet too large.” MySQL packet too large. (dev.mysql.com)

I agree with : storing audio in a DB via BLOBs is valid. Filesystem + DB metadata also works; choose based on backup/transaction needs.

Recommended Answers

All 4 Replies

"display audio on jsp" ...
the type of application you work with isn't relevant, whether it's a standalone using swing or javafx or a webapp using gwt or jsp/jsf, the persistence is not impacted by this.

"but cannot upload only upto amount of kbs" what exactly do you mean by that?
and what do you mean by "display audio on jsp"?

the audio files that are stored, i want them to be retrived and play and them when user clicks on them.

sorry, you're dead wrong. Blobs are the way to store binary data in a relational database.
AND audio files are far from unstructured.

the audio files that are stored, i want them to be retrived and play and them when user clicks on them.

Take a look at JSF (you want it instead of JSP anyway) and especially Primefaces which has audioplayer components.
Other JSF libraries may have them as well, but Primefaces is very good and pretty much accepted across the industry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.