Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2004, 04:20 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default Uploading a file using JSP

I have to upload a file from client browser to server database(not in any folder) using JSP-servlets(without struts). How can I do this? Can you provide me a simple code snippet?
The Following User Says Thank You to Inderjeet For This Useful Post:
 
Old October 20th, 2004, 02:28 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can send data from a standard HTML form if you want, it doesnt have to be JSP, you need to use the POST method within the form.

You will need to write a servlet to parse the data (removing headers added by the post action) back to its original form.

I found the following tutorial helpful, there is a section "reading get and post values" about half way down.

There is an error in the source code given in one of the loops, but its a good start:

http://java.sun.com/developer/online...k/aucserv.html

 
Old October 20th, 2004, 11:35 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi
I have gone through the link sent by you. but it doesn't provide the solution to store the file in the database, it just gives the method to upload a file. And JSP is my requirement, so i have to do this on JSP only.

 
Old October 25th, 2004, 06:58 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont think youll find one solution to this, I couldnt, but I did get images uploaded into a database.
First the form POSTED the data to a servlet.
This servlet put the data into a byte array named fileBytes:

byte fileBytes[]=new byte[27000];
System.arraycopy(tmpbuffer, 0, fileBytes, offset, length);

I then put the data into a byte array input stream. The number parameters are the start and end characters.

bin = new ByteArrayInputStream( fileBytes, 2, offset-2 );

I then put this data into a normal input stream:

InputStream is = bin;

Then used the following as part of a prepared statement:

pstmt.setBinaryStream( 5, is, (int)(len));

Its a bit of a game but it works.

 
Old October 25th, 2004, 02:21 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again, just remembered this JSP example, not sure if it will work first time as ive messed around with it. Found the basis of it on the Sun Java site. The person who posted it wanted to know why it would only work on their local machine, not the server. The answer is that this uses the GET method, so only passes the file path/name. This is ok on a local machine as the file can be found, but less useful when passed to the server.

This will allow you to get a test working which poulates a database, but you will need to use the info in my previous posts including the tutorial if you want to get it working on a client/server basis.

If you cant get this working look for original thread on Sun Java site.

<%@ page contentType="image/jpeg; chaoResult=iso-8859-1" language="java" %>
<%@ page session="false" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.io.*" %>

<html>
<body>

<%
    File img= new File(request.getParameter("image"));

    String databaseDriver = "org.gjt.mm.mysql.Driver";
    Class.forName( databaseDriver );
    String databaseName = "jdbc:mysql://localhost/test";
    Connection con = null;
    try{
        con = DriverManager.getConnection(databaseName, "dBt","");
    //response.setContentType("image/jpeg");
        String str = "INSERT INTO images (id, image) VALUES ?,?)";
        PreparedStatement pstmt = con.prepareStatement(str);

pstmt.setString(1, "test");
InputStream is = new FileInputStream(img);
pstmt.setBinaryStream( 2, is, (int)(img.length()));
//set the Blob
pstmt.executeUpdate();
is.close();




   pstmt.close();
    pstmt = null;
    }

    finally {
        if (con != null )
        {
            con.close();
        }
    }

%>
</BODY>
</HTML>


 
Old January 19th, 2005, 12:07 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am also interested in this topic. but i think that topic is not the end of the story.

I would also like to know how to retrive the file from database.
Now, I know how to change the file into file stream and store in blob database, but how can I make use of it? like allow user to download or save it back into file or retrive it and do process base on it?

I am using mysql database and JSP to handle the request.

Thanks~~

 
Old January 19th, 2005, 05:48 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is not the end of the story!

This thread shows in a rough fashion how to get images out of a database and display them in a JSP page.

http://forum.java.sun.com/thread.jsp...hreadID=505366

Expanding on this you can manipulate the image, this link shows how to watermark and blend images. This is the best code I have ever found in a forum!!

http://forum.java.sun.com/thread.jsp...hreadID=505366

All you have to do is write a servlet that tie's it all together hehe.


 
Old January 21st, 2005, 04:33 AM
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hey all,
please give me the sample JSP source code to upload file as object into database.

thanks,
rahul
 
Old January 22nd, 2005, 12:30 PM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to bditt
Default

Uhm....ever think of using the JSTL 2.0 sql taglib? I don't see why you couldn't use a <sql:update after a file was selected in a form....com'on guys, you don't need someone to hold your hand every step of the way. I'd personally use webwork2 and the interceptors to get files. http://www.opensymphony.com/webwork/...terceptor.html

Brian
 
Old January 22nd, 2005, 09:41 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have been using beans to perform sql transactions and return data to my jsp pages, not sure if this is the best approach, but as I will need to put the same program on different servers it seemed easier to keep it all in java as opposed to using jsp / jstl

Liked the link you posted, reckon Ill incorporate some of these methods into my servlet, thanks.






Similar Threads
Thread Thread Starter Forum Replies Last Post
uploading a file MunishBhatia ASP.NET 2.0 Professional 4 May 30th, 2007 04:18 AM
Uploading File misskaos Classic ASP Basics 4 October 26th, 2006 02:56 PM
file uploading lakshmi devi Classic ASP Basics 2 September 3rd, 2006 11:52 PM
FIle Uploading [email protected] Classic ASP Basics 3 February 23rd, 2004 12:32 PM
File uploading zabedin Classic ASP Basics 1 July 16th, 2003 08:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.