upload images in the database -
i have been trying upload image table no joy @ all. have used store procedure , encrypted connection strings in appsettings , use data layer access objects.
string filepath = fileupload1.postedfile.filename; string filename = path.getfilename(filepath); string ext = path.getextension(filename); string contenttype = string.empty; switch (ext) { case ".jpg": contenttype = "image/jpg"; break; case ".png": contenttype = "image/png"; break; case ".gif": contenttype = "image/gif"; break; } if (contenttype != string.empty) { stream fs = fileupload1.postedfile.inputstream; binaryreader br = new binaryreader(fs); byte[] bytes = br.readbytes((int32)fs.length); sqlcommand _sqlcom = new sqlcommand("sp_insfoto"); _sqlcom.parameters.add("@imgname", sqldbtype.varchar).value = filename; _sqlcom.parameters.add("@imgdata", sqldbtype.binary).value =bytes; _sqlcom.parameters.add("@imgcontenttype", sqldbtype.varchar).value =contenttype; obj.executenonquery(ref _sqlcom);
and have store procedure stated below
create procedure [dbo].[sp_insfoto] ( @imgname varchar(50) ,@imgdata varbinary(max) ,@imgcontenttype varchar(50) ) insert tbl_fotos ( imgname ,imgdata ,imgcontenttype ) values ( @imgname ,@imgdata ,@imgcontenttype )
**uploading image , saving path in database ________________________________________________________________________________________ <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <div> <h3> choose file upload in server </h3> <form action="recent" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="upload" /> </form> </div> </body> </html> ____________________________________________________________________________________________ import java.sql.*; import java.io.*; import javax.servlet.servletexception; import javax.servlet.annotation.multipartconfig; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.part; import javax.servlet.annotation.multipartconfig; import javax.servlet.annotation.webservlet; import java.util.hashtable; import java.util.list; import org.apache.commons.fileupload.fileitem; import org.apache.commons.fileupload.parameterparser; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; /** * servlet implementation class recent */ @webservlet("/recent") @multipartconfig public class recent extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public recent() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub sample s1=new sample(); final string upload_directory = "/home/pradeep/documents/pradeep/webcontent/images"; if(servletfileupload.ismultipartcontent(request)){ try { list<fileitem> multiparts = new servletfileupload(new diskfileitemfactory()).parserequest(request); for(fileitem item : multiparts){ if(!item.isformfield()) { string name = new file(item.getname()).getname(); item.write( new file(upload_directory + file.separator + name)); string path= "/home/pradeep/documents/pradeep/webcontent/images/" +name; s1.connecting(path); } } request.setattribute("message", "file uploaded successfully"); } catch (exception ex) { request.setattribute("message", "file upload failed due " + ex); } }else{ request.setattribute("message", "sorry servlet handles file upload request"); } request.getrequestdispatcher("/result.jsp").forward(request, response); } } __________________________________________________________________________________________ import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import dbconnections.connections; public class sample { connections con=new connections(); public void connecting(string path) { connection conn=con.connect(); preparedstatement pst; string query="insert student1 (path) values (?)"; try { pst=conn.preparestatement(query); pst.setstring(1,path); pst.executeupdate(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } // todo auto-generated method stub } }
Comments
Post a Comment