Reputation:
As i mentioned in my title, i want to upload image into the database field. So, whatsoever I have done with my coding part. I'm sharing here. I don't think, there would be any problem with my code, since, i'm getting right output at the screen. But, the problem is, when i opened database, it showing some different output. I don't know, what is wrong in the database, when I try to execute select * from PIC
. Then, PIC
column shows some unexpected result. Don't know, whether my image is inserting into the table or not.
Thanks !!!
index.jsp
<html>
<head>
<title>Upload</title>
</head>
<body>
<form action="upload" enctype="multipart/form-data" method="post">
<table width="400" align="center">
<tr><td>UserName :</td><td><input type="file" name="userid" id="userid" /></td></tr>
<tr><td><input type="Submit" value="Upload" style="width: 100px; height:30px;"/></td></tr>
</table>
</form>
</body>
This my servlet page:
upload.java
package fileupload;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.regex.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class upload extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
out.println("items: " + items);
} catch (FileUploadException e) {
e.printStackTrace();
}
FileItem file = (FileItem) items.get(0);
// out.print(file);
Connection conn = null;
String connstr = "jdbc:oracle:thin:@localhost:1521/XE";
String uname = "epolicia";
String password = "admin";
String driver = "oracle.jdbc.OracleDriver";
String sql = null;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(connstr, uname, password);
// FileInputStream fs = new FileInputStream(savedFile);
sql = "INSERT INTO pic(pic) VALUES(?)";
PreparedStatement st = conn.prepareStatement(sql);
// st.setInt(1,1);
st.setBinaryStream(1, file.getInputStream(), (int) file.getSize());
int rs = st.executeUpdate();
if (rs > 0) {
out.println("Query Executed Successfully++++++++++++++");
}
conn.close();
} catch (Exception e) {
out.println(e.getMessage());
}
}
}
}
Pages are successfully running without any exception. Help will be appreciated.
Upvotes: 1
Views: 22560
Reputation: 19
U can use data type Midblob or blob in mysql. to store image. midbolb data type stores large size morethan blob ..
Upvotes: 1
Reputation:
Finally, I found the solution. Thanks to everyone. I followed the steps which are mentioned in the website, the link, and I'm sharing the same links. So that, others will get the benefits of this.
Upload photos into Oracle BLOB column from Java Servlet and listing photos
Since, image is being inserted in blob
type, which binary large object
, which can not displayed on that sql*plus
. That's why was showing [datatype]
.
Upvotes: 4