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