Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2005, 09:53 PM   PM User | #1
punnkdork
New to the CF scene

 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
punnkdork is an unknown quantity at this point
(Java)Client to server fileupload using commons into database - beginner

The problem: I wrote code using commons fileupload in a servlet which worked until I moved the code into production causing the file path(from client browser) to not exist(on sever). Doh.

So, I posted a message on a board asking for some help and someone stated I should use the file input stream. I have been reading and searching so many articles, tutorials and messageboard threads that I am now completely confused and intimidated by this. I dont know what I am doing wrong or where to start!

What I need to get accomplished is this: The should be able to select a file off their local machine and upload it into the database - the actual file will upload in binary format and reside in the db.

There doesnt seem to be an abundant amount of examples of this out there or if there are I havent been able to find them.

1.) First of all, I dont really understand how the client and server relationship works. If I get a local machines' path and try to process this on the server side(via a servlet) then that path will not exist, correct?

2.) If the previous statement is true - then would I not want to process the fileupload in a servlet at all? Or, even how can I resolve the actual path to use. Is this even possible? This just confuses me.

I think I have the right code to upload the file and save it as a binary blob in the database. It works but just not from a client machine.

html
Code:
<form name="getAttch" onSubmit="return chkForm()" 
ACTION="/<c:out value="${param.newID}"/>/addAttachment"
method="POST" enctype="multipart/form-data">
<input class="frmButtons" type="file" name="userUpload" TABINDEX="7" size="80"> 
<input class="frmButtons" type="submit" name="getAttch" value="Upload File">
</form>
upload code
Code:
        String tempPath = null;
        String ootNum = null;
        String docPath = null;
        String content_type = null;
        File uploadedFile = null;
        FileInputStream file = null;
        
        HttpSession session = request.getSession(); 
        
        try {
            
            DiskFileUpload upload = new DiskFileUpload();
            List items = upload.parseRequest(request);
            Iterator iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    InputStream is = item.getInputStream();
                    if (item.isFormField()) {
                        String name = item.getFieldName();
                        String value = item.getString();
                            if(name.equals("OOTNum")) {
                                ootNum = value;
                            }
                    } else {
                        content_type = item.getContentType();
                        long sizeInBytes = item.getSize();
                        docPath = item.getName();
                        uploadedFile = new File(docPath);
                        item.write(uploadedFile);
                        file = new FileInputStream(uploadedFile);
                    }
                    
                }
                }
save to db
Code:
         ...
        this.conn = database.SybaseDAO.grabConnection();
        String query2 = "INSERT INTO
        MT."+newID+"_OOT_ATTACHMENTS(OOTNum, attachBlob,
        attachName, attachExt, attachContentType) VALUES(?,?,?,?,?)";
        ps = conn.prepareStatement(query2);
 
        ps.setString(1, ootNum);
        ps.setBinaryStream(2, file, (int)uploadedFile.length());
        long fileLength = uploadedFile.length();
        addInformation.setFileLength(fileLength);
        ps.setString(3, docName);
        ps.setString(4, docExt);
        ps.setString(5, content_type);
        ps.executeUpdate();
        ps.close();
        ps = null;
Some of my references:
I have followed this example for my code:
http://jakarta.apache.org/commons/fileupload/using.html

and I found this one(but I seem to be doing the same thing as they are):
http://www.experts-exchange.com/Web/..._21278328.html

and this one(which I wonder if I should be doing):
http://www.devx.com/getHelpOn/Articl...8/1954?pf=true

I am sorry if this is really long but I am overwhelmed by this at this point. Any help getting me on the right track would help. Thanks

Crystal

Last edited by punnkdork; 10-10-2005 at 10:13 PM.. Reason: eta:
punnkdork is offline   Reply With Quote
Old 10-11-2005, 08:18 AM   PM User | #2
suryad
Regular Coder

 
Join Date: Apr 2005
Location: Folsom, CA
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
suryad is an unknown quantity at this point
I am assuming you are using Tomcat? What OS are you using? Have you tried setting the context in the admin section of the Tomcat server? Because from your post I understand that you are having a problem with the path correct?
suryad is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:38 AM.


Advertisement
Log in to turn off these ads.