I want to accomplish the following scenario:1) user submits a jpg/gif file from an html form (using <input type="file"> in the form, the user can browse to a file of their choice)2) upload the jpg/gif file through a combination of Java and JSP's3) put the jpg/gif into a database on a server and/or into a folder on my local system.

Showing Answers 1 - 1 of 1 Answers

Amit Kumar

  • Feb 7th, 2006
 

<%@ page import="java.sql.*,java.io.*,java.util.*,com.oreilly.servlet.MultipartRequest,com.microsoft.jdbc.sqlserver.SQLServerDriver;"%>
<%

/* The Following Code is Used To Insert An Image Into Database

String filename="";
try
{
MultipartRequest multi= new MultipartRequest(request,"d:/phani",5*1024*1024);
Enumeration files=multi.getFileNames();
File f=null;
while(files.hasMoreElements())
{
String name=(String)files.nextElement();
filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
f=multi.getFile(name);
System.out.println("The File is "+f);
}
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://172.21.1.11:1740;databasename=pubs","sa","satest");
Statement stmt = con.createStatement();
System.out.println("2 "+f);
InputStream is = new FileInputStream(f);
System.out.println("4 "+is);
byte b[]=new byte[is.available()];
is.read(b);
String sql = "INSERT into photo_test (\"Photo\") values('" + b + "')";
System.out.println("sql is " +sql);
stmt.execute(sql);
stmt.close();

}catch(Exception e)
{
System.out.println(e);
}
out.println("The Image is Added into Database");

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.