aspose file tools*
The moose likes Servlets and the fly likes uploading..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "uploading....." Watch "uploading....." New topic
Author

uploading.....

Friend
Ranch Hand

Joined: Nov 23, 2000
Posts: 67
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

Joined: Feb 04, 2002
Posts: 231
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

Joined: Nov 27, 2001
Posts: 1561
I use jsmsmartupload class. www.jspsmart.com. And yes, it's free.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 264
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

Joined: Aug 17, 2001
Posts: 399
hi there
u can even use com.oreilly package by jason hunter from www.servlets.com
hth
malhar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uploading.....
 
Similar Threads
JUnit?
uploading images
Upload attachment using Java
Java AntiVirus Scan for File Upload
HttpClient file upload code review