0

I've been having some trouble performing this task and I could use a little help:

Im trying to upload a picture from my filesystem to a MYSQL DB using a JSP/Java Servlet

I have a file in an images folder. I know I'm supposed to read the file, convert it into a byte, get the outputStream, but I have had little luck doing so (and I've posted no code because my attempts have been train wrecks). After the file is in the outputStream, I know how to form a sql statement as an insert with a blob referenced as a ? parameter, but I cannot get this far.

Any help would be much appreciated.

Deprecated
  • 163
  • 1
  • 3
  • 14
  • Are you uploading to the server, or downloading to a client? It's really not clear. Either way, there are dozens of examples-Please ask a question about a specific issue you're having. – Dave Newton Mar 27 '13 at 01:32
  • If there are dozens of examples, could you please post one? I haven't been able to find a single one where you have found dozens. – Deprecated Mar 27 '13 at 01:59
  • I don't see how that's possible: just search for Java +store image in database. (Or the opposite of you're trying to stream it back.) Easy search. – Dave Newton Mar 27 '13 at 02:00
  • possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet) This kind of question has been asked zillions of times. – Luiggi Mendoza Mar 27 '13 at 02:29
  • first you need to show your research effort. i.e; show your code snippets,if there is any wrong experts will help you. – Arun Kumar Mudraboyina Mar 27 '13 at 06:02

2 Answers2

0

steps you need to follow 1. use input type="file" tag in your main view. 2.using DiskFileItemFactory read all the bytes of uploaded file 3.keep the file in server's folder 4.identify the file with the file name from this folder location and store it into MySql DB for this use blob 5.dont directly pick the file from your local system and storing in the DB,first of all you have to upload it into your server and then perform DAO operation

0

public class UploadFilesServlet extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();


try
{
//step1
DiskFileItemFactory df=new DiskFileItemFactory();
//step2
df.setSizeThreshold(10000); //setting buffersize    
String temp=getServletContext().getRealPath("/WEB-INF/temp");
df.setRepository(new File(temp)); //if buffer crossed comes into the temp
//step3
ServletFileUpload sf=new ServletFileUpload(df);
//step4
List<FileItem> items=(List<FileItem>)sf.parseRequest(req);
//step5
for(FileItem item: items)
{
if(item.isFormField())
{
//this item is a simple text field
String name=item.getFieldName();
String value=item.getString();
pw.println(name+"="+value+"<br/>");
}
else
{
//this is a file
String name=item.getFieldName();
String fileName=item.getName();


if(fileName.lastIndexOf('\\')!=-1)
fileName=fileName.substring(fileName.lastIndexOf('\\')+1);

fileName=getServletContext().getRealPath("/WEB-INF/upload/"+fileName);
item.write(new File(fileName));
pw.println("file:"+fileName+"saved</br>");
      BlobDemo.saveFile(fileName);

}//else
}//for

}catch(Exception e){e.printStackTrace(); }
    }
    }

this code places the client's file into WEB_INF/upload folder ,after the file uploading locate the file using the same path and use the streams and blob data types to store the file with its file name.

public class BlobDemo {
private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static String username = "kodejava";
private static String password = "welcome";

public static void saveFile(String fileName)throws Exception {
    Connection conn = null;
    FileInputStream fis = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);

        String sql = "INSERT INTO Files_Table(name,  file) VALUES (?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, fileName);


        File file = new File("WEB-INF\\upload\\"+fileName);
        fis = new FileInputStream(file);
        stmt.setBinaryStream(2, fis, (int) file.length());
        stmt.execute();

        conn.commit();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }
}

}