Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
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 of mysql it does not have anylongblob.
so now can anyone help me upload audio,display them on jsp and play them after selecting them.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900