Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

uploading.....

 
Friend
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please give me , how I write code for uploading files in java. I am using java, sevlet,html for developing source code.
thanks a lot.
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very repeated question in this forum. Search the forums before posting is a good practice.Here you have some related threads:
http://www.coderanch.com/t/355011/Servlets/java/Sample-servlet-handling-file-upload
http://www.coderanch.com/t/347821/Servlets/java/file-upload-servlet
[ September 12, 2002: Message edited by: Juanjo Bazan ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use jsmsmartupload class. www.jspsmart.com. And yes, it's free.
 
Graham Thorpe
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the code for inserting imahe files suing java servlets
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
//public class WriteBlob
public class WriteBlob extends HttpServlet
{
// private static Connection con = null;
private static PreparedStatement ps;
//public static void main(String[] argv)
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
try
{
String name=req.getParameter("text1");
String type=req.getParameter("text2");
String file1=req.getParameter("file");
Connection con = null;

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con=DriverManager.getConnection("jdbc racle:thin:@oracle:1521 RCL","scott","tiger");
/*Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:img","sa","");*/
//File file =new File("fevicon.ico");
File file =new File(file1);
InputStream is = new FileInputStream(file);
ps =con.prepareStatement("insert into emp_photo VALUES(?,?,?)");
ps.setString(1,name);
ps.setString(2,type);
out.println("name is" +name+"<br>");
out.println("type is" +type);
out.println("pic is" +file1);
out.println("Inserted Succesfully***************..");
ps.setBinaryStream(3,is,Integer.MAX_VALUE);
out.println("Inserted Succesfully..");
ps.execute();
out.println("Inserted Succesfully..");
con.close();
out.println("<a href=http://subra:7001/GetBlob><b>Image</b></a>");
out.println("<br>");
out.println("<a href=http://subra:7001/img.jsp><i>Home</i></a>");
//res.sendRedirect("http://subra:7001/img.jsp");

}
catch (Exception ex)
{
ex.printStackTrace(out);
}
}
}
This is the code for retreving images from database

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetBlob extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
try
{
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con =DriverManager.getConnection("jdbc racle:thin:@oracle:1521 RCL","scott","tiger");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT picture from emp_photo where empno = 'red' and photo_format='jpg' ");
if(rs.next())
{
InputStream is = rs.getBinaryStream(1);
InputStreamReader isr=new InputStreamReader(is);
BufferedReader bfr = new BufferedReader(isr);
String str=null;

while((str=bfr.readLine())!=null)
{
out.println(str);
}
}

rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
Malhar Barai
Author
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there
u can even use com.oreilly package by jason hunter from www.servlets.com
hth
malhar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic