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 July 9th, 2003, 07:23 AM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get File type before save to database.

Hello,

How to get File type before save to database in a JSP page.

One confusion I have, while I am trying to upload file from Client machine and saving it to DataBase,

From saving the file to database I am using Prepared Statement here for

[1] for image type file we are using setBinarySream()
[2] for txt or doc file type we are using setAsciiSream()

* please tell is some general stream format is for all type of files, or we have to set Stream according to file.

* if different file is having different stream then please tell me how we can get the type of file befoe uploading so that we will use the right Stream while before save to database.

Please tell me with code.
Do replay
Regards.
 
Old July 19th, 2003, 05:00 AM
Authorized User
 
Join Date: May 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
Answer to some of your Quires
Q.How to get File type before save to database in a JSP page.
Ans. When the browser send the content of a file it also send file information and the content type of the file. So to get the content type or the file type one has to parse this information. Following is the code which extracts file name.
ServletInputStream readstream = req.getInputStream();
int i,number=0;
while((i = readstream.readLine(data,0,data.length)) != -1)
{
    String str1 = new String(data);
    str1 = str1.trim();
    if(str1 == null)
        break;
    //This if block extracts the file name
        if(str1.startsWith("Content-Disposition:"))
    {
        int beginpos = (str1.indexOf("filename="));
        int endpos = (str1.length());
                String pathname = str1.substring(beginpos,endpos);
        int namepoint = pathname.lastIndexOf('\\');
        int nameendpoint = pathname.lastIndexOf('\"');
        String namef = pathname.substring((namepoint+1),(nameendpoint));
        setfilename(namef);
        if (createFile())
        writedata = true;
    }

Q. please tell is some general stream format is for all type of files, or we have to set Stream according to file.
Ans. I suggest you can use setBinaryStream()for all the files.

Yashraj Chauhan
Java\J2EE Specialist
Wiley Support Team





Similar Threads
Thread Thread Starter Forum Replies Last Post
Save database schema to XML file brettk_1 General .NET 2 September 16th, 2006 04:27 AM
Save xlsheet file in a database nachi_hota ADO.NET 1 August 4th, 2006 05:08 AM
Save BINARY FILE in ACCESS database gasma1975 Access 4 February 10th, 2005 12:16 PM
<input type="File"> - Specify File Type and Path gp_mk Classic ASP Basics 2 August 2nd, 2004 03:07 AM
How to get File type before save to database monika_jain_99 J2EE 0 July 9th, 2003 07:12 AM





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