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 February 17th, 2005, 12:36 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been working at this task for 3 hours and finally I found out a solution without using other packages like the ones from Jakarta or O'Reilly. Below you have my code. Test it. This jsp writes directly on the hdd but if you want to write on a server database you just put th e variable "continut" in the right blob field and that's all. Good luck using it.

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.sap.dbtech.powertoys.*" %>
<%@ page import="com.sap.dbtech.rte.comm.RTEException" %>

<html>
<head>
<title>Document de UPLOAD</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<%
  String cale = request.getRealPath("/") + "/";
  String deScris = "";
  DataInputStream in = null;
  DataOutputStream output = null;

  try
  {
    in = new DataInputStream(request.getInputStream());

    int contor = 0;
    //preluam boundary
    String linie = in.readLine();
    String boundary = linie;
    contor+=2*boundary.length();
    out.println("BOUNDARY: " + linie + "<br>");

    //se obtine numele fisierului
    linie = in.readLine();
    contor+=linie.length();
    String numeFisier = linie.substring(linie.indexOf("filename=") + 10);
    String numeFisier_ok = numeFisier.substring(0, numeFisier.length() - 1);

    out.println("NUMELE FISIERULUI ESTE: " + numeFisier_ok + "<br>");
    deScris = cale + numeFisier_ok;
    out.println("SE SCRIE FISIERUL: " + deScris);
    output = new DataOutputStream(new FileOutputStream(deScris));

    //se citeste linia pe care se afla tipul continutului
    linie = in.readLine();
    contor+=linie.length();

    //se citeste o linie goala
    linie = in.readLine();
    contor+=linie.length();

    //aici citim octetii de continut
    int lungimeFisier = request.getContentLength() - contor;
    out.println("LUNGIMEA FISIERULUI ESTE: " + lungimeFisier + "<br>");

    byte[] continut = new byte[request.getContentLength() - contor];
    in.readFully(continut);

    output.write(continut);
    output.flush();
  }

  catch(IOException e)
  {
    out.println("EROARE FATALA: " + e + "<br>");
  }

  finally
  {
    if(in != null)
     in.close();

    if(output != null)
     output.close();
  }
%>

</body>

</html>

 
Old February 17th, 2005, 03:06 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Looks simpler than the approach I took using servlets, will have a play with it.



 
Old February 24th, 2005, 09:27 AM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

About 18 months ago I had to learn how to do this. A few search results talked about the slowness of uploading/retrieving Blobs from databases. One in particular mentioned PostgreSQL, the database I was using. I needed to upload images which had to be available for web pages.

In the end the solution was to write them to disk on the server and store the filename and path as a string in the database. This has worked well for quite a few web-applications. Have a look at http://www.cars4us.com.au All the vehicle photos are stored this way. At present about 1500-2000 for that particular web-application. The thumbnails are created on the server from the uploaded photos.

If anyone is interested I will give code examples. The upload bean can load any number of files and text elements in any sequence.

Chris Malan
 
Old February 24th, 2005, 04:57 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am aware of this method, but there are a couple of disadvantages.

One was that I did not want a folder containing thousands of images, keeping the images in a database seemed neater, though I suppose it doesnt really matter.

The main disadvantage as I saw it was that you would need additional code to find/return the images for display, and also to delete them.

If everything is in the database then you simply delete the record, you do not need to then go and delete a sepereate image.

I dont know about the issue with speed, I havent tried this on a large scale yet. The speed when I used it was tolerable, though I would imagine that saving to disk would be faster.

 
Old February 24th, 2005, 09:30 PM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>>The main disadvantage as I saw it was that you would need additional >>code to find/return the images for display, and also to delete them.

>>If everything is in the database then you simply delete the record, >>you do not need to then go and delete a sepereate image.

You are absolutely right. The display is very little more code. The images are deployed in a folder mapping to /photos in JBoss - the JBOSS/server/default/deploy/photos.ear/photos.war folder. To display a photos is as simple as http://www.mysite.com/photos/piccie.jpg

Deleting is more work. I have a file filter to filter out images by user who uploaded them. This way this user can only delete/replace/update his own images. I also check the image size before displaying them. Some bozos think nothing of uploading a great many 2MB jpgs. A message on the page supposed to display there photos to the effect that their photos are too big and won't be displayed usaully sets them thinking. Unfortunately there is no easy way to check photo size on the client computer before uploading. I suppose active-x can do it. JavaScript does not want to read things from the cliets disk - a security constraint.

A mercy is that the code has only to be written once. Mine works, thanks be to God, and I re-use it over and over.

Chris Malan
 
Old February 26th, 2005, 07:23 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can limit the size of a file at upload by setting a smaller buffer size, and adding exception handling if this buffer is exceeded. I am not sure if you can check the file size before filling the buffer, someone told me you could, but for this the size would have to be passed in the header details, something I have never observed. This would save bandwidth.

i.e. I throw out large files after they have exceeded the upload limit. I would like to throw them out before they have consumed my bandwidth.

 
Old March 2nd, 2005, 04:56 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

May i have the codes to upload multiple files to a file server, an save the details of the files in a db?

Thanks......

Quote:
quote:Originally posted by chrismalan
 About 18 months ago I had to learn how to do this. A few search results talked about the slowness of uploading/retrieving Blobs from databases. One in particular mentioned PostgreSQL, the database I was using. I needed to upload images which had to be available for web pages.

In the end the solution was to write them to disk on the server and store the filename and path as a string in the database. This has worked well for quite a few web-applications. Have a look at http://www.cars4us.com.au All the vehicle photos are stored this way. At present about 1500-2000 for that particular web-application. The thumbnails are created on the server from the uploaded photos.

If anyone is interested I will give code examples. The upload bean can load any number of files and text elements in any sequence.

Chris Malan
 
Old March 2nd, 2005, 03:18 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont have the code in a form that I can easily post, however O Riely (the book publisher) have made classes available to facilitate multiple file upload.

The classes and documentation are available at:

http://servlets.com/cos/

Should make life easier for you!

 
Old March 29th, 2005, 11:31 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default



Here you go!!!
I have done this for 5 images to be uploaded, but it could be +++


<%@ page import="
        java.io.*,
        java.util.*,
        java.text.*,
        java.io.*,
        java.rmi.RemoteException,
        java.rmi.Remote,
        javax.naming.*,
        javax.ejb.*,
        javax.servlet.*,
        javax.servlet.http.*,
        javax.rmi.PortableRemoteObject,
        com.oreilly.servlet.multipart.*,
        com.nationstreet.ejb.orderManager.*"
%>
<%!
    boolean UploadImagesActivity(byte[] dataArray)
    {
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "INSERT INTO hdusa_damagedgoods(img_file) values(?)";

        try
        {
            con = getConnection();
            ps = con.prepareStatement(sql);
            ps.setBytes(1, dataArray);
            ps.executeUpdate();
        }
        catch (SQLException e)
        {
            System.out.println("SQL: " + sql);
            e.printStackTrace();
            return false;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
        finally
        {
            try
            {
                if (ps != null)
                    ps.close();
            }
            catch (Exception e) {}
            try
            {
                if (!con.isClosed())
                    con.close();
            }
            catch (Exception e) {}
            ps = null;
            con = null;
        }
        return true;
    }

%>
<%

    int maxFileSize = 20 * 1024 * 1024; // 20MB max
    MultipartParser parserFormData = new MultipartParser(request, maxFileSize);
    Part p = null;
    long numBytes = 0;
    byte[] fileDataArray1 = null;
    byte[] fileDataArray2 = null;
    byte[] fileDataArray3 = null;
    byte[] fileDataArray4 = null;
    byte[] fileDataArray5 = null;

    try
    {

        while((p = parserFormData.readNextPart()) != null)
        {

            if(p.isFile())
            {
                FilePart fp = (FilePart)p;
                    InputStream fip = fp.getInputStream();
                    ByteArrayOutputStream bias = new ByteArrayOutputStream();

                        if(fp.getFileName() != null)
                        {
                        int intSize = 0;
                            while((intSize = fip.read()) != -1)
                            {
                                bias.write((byte)intSize);
                            }

                            if (p.getName().equals("image1") && !fp.getFileName().equals(""))
                            {
                            fileDataArray1 = bias.toByteArray();
                            }
                            if (p.getName().equals("image2") && !fp.getFileName().equals(""))
                            {
                            fileDataArray2 = bias.toByteArray();
                            }
                            if (p.getName().equals("image3") && !fp.getFileName().equals(""))
                            {
                            fileDataArray3 = bias.toByteArray();
                            }
                            if (p.getName().equals("image4") && !fp.getFileName().equals(""))
                            {
                            fileDataArray4 = bias.toByteArray();
                            }
                            if (p.getName().equals("image5") && !fp.getFileName().equals(""))
                            {
                            fileDataArray5 = bias.toByteArray();
                            }
                        }
            }
        }

        if(fileDataArray1 != null && fileDataArray1.length > 0)
        {
            UploadImagesActivity(fileDataArray1);
        }

        if(fileDataArray2 != null && fileDataArray2.length > 0)
        {
            UploadImagesActivity(fileDataArray2);
        }

        if(fileDataArray3 != null && fileDataArray3.length > 0)
        {
            UploadImagesActivity(fileDataArray3);
        }

        if(fileDataArray4 != null && fileDataArray4.length > 0)
        {
            UploadImagesActivity(fileDataArray4);
        }

        if(fileDataArray5 != null && fileDataArray5.length > 0)
        {
            UploadImagesActivity(fileDataArray5);
        }

    }
    catch (Exception e)
    {
         e.printStackTrace();
    }
%>


 
Old March 31st, 2005, 06:25 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sh_sal79
Default

Hi chrismalan,

  Could you please send me the code of creating thumbnails of jpeg image in Jsp...my E-id is [email protected]

sh_sal79

Quote:
quote:Originally posted by chrismalan
 About 18 months ago I had to learn how to do this. A few search results talked about the slowness of uploading/retrieving Blobs from databases. One in particular mentioned PostgreSQL, the database I was using. I needed to upload images which had to be available for web pages.

In the end the solution was to write them to disk on the server and store the filename and path as a string in the database. This has worked well for quite a few web-applications. Have a look at http://www.cars4us.com.au All the vehicle photos are stored this way. At present about 1500-2000 for that particular web-application. The thumbnails are created on the server from the uploaded photos.

If anyone is interested I will give code examples. The upload bean can load any number of files and text elements in any sequence.

Chris Malan





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.