This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileUpload class not working  RSS feed

 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.

I'm trying to create a custom FileUpload class but the file isnt being uploaded to its target. Code as follows:



I'm not getting any compilation errors or runtime exceptions. It just wont upload the file. Any ideas?
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No runtime errors? But you're catching them all and storing them; are you sure your code is actually looking at them and telling you they exist?

If so then it looks like line 80 isn't ever being executed. If you work your way back up the code you could look at all of the if's and while's which you are assuming act the way you think they should act, and perhaps they are acting differently for some reason which you have overlooked. It's also possible that you have misunderstood what folder the files are being written and you're looking in the wrong place for them.

 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No runtime errors? But you're catching them all and storing them; are you sure your code is actually looking at them and telling you they exist?

If so then it looks like line 80 isn't ever being executed. If you work your way back up the code you could look at all of the if's and while's which you are assuming act the way you think they should act, and perhaps they are acting differently for some reason which you have overlooked. It's also possible that you have misunderstood what folder the files are being written and you're looking in the wrong place for them.


I have this class which invokes a FileUpload object:



And the FormProcessor class code is as follows:



Hope thats any help...
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EditProfile object works but not the FileUploader()
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If this variable remains null then the upload won't be done. I don't see any code which sets its value, but then you didn't post what you said you posted just now...
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

If this variable remains null then the upload won't be done. I don't see any code which sets its value, but then you didn't post what you said you posted just now...


so change it to:



???
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

If this variable remains null then the upload won't be done. I don't see any code which sets its value, but then you didn't post what you said you posted just now...


Sorry that variable is set on line 73
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heres a complete run down of the exact execution of the whole thing...

index.class



FormController.class:



FormProcessor.class



FileUploader.class



The form data gets through to the database but the file wont upload. Sorry if i posted a lot but I wanted to get everything in. Help appreciated.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
....Sorry FileUploader is...



i made a typo in the last post
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No ideas anybody? I really need to this fixing as I'm ripping my hair out over it! It should be working but it just isn't
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ive got an update. I've got it working but it appears the FileUploader object will only work within a Servlet. If I pass the HttpServletRequest object into a different class it doesnt work. Is there a way around this? cheers
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. You've posted a lot of code but you haven't said how it's called. For example it's not obvious in which order the various methods are called. Presumably the data ultimately comes from a servlet request, and passing an HttpServletRequest reference as a parameter shouldn't cause any problems. However storing that reference until after the request has been completely processed might cause problems, I suppose. So did you do that?

By the way your use of capitalized names like SwitchFormProcess for method names, I find that very distracting. The standard of lower-casing method names (like switchFormProcess) is so widespread that it's actually hard for me to follow programs that don't use that standard.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Okay. You've posted a lot of code but you haven't said how it's called. For example it's not obvious in which order the various methods are called. Presumably the data ultimately comes from a servlet request, and passing an HttpServletRequest reference as a parameter shouldn't cause any problems. However storing that reference until after the request has been completely processed might cause problems, I suppose. So did you do that?

By the way your use of capitalized names like SwitchFormProcess for method names, I find that very distracting. The standard of lower-casing method names (like switchFormProcess) is so widespread that it's actually hard for me to follow programs that don't use that standard.


Ok lets start again and let me explain the entire process of whats going on...

On my index servlet I have the following code:



And my FileUploader.class code:



When I create an object of FileUploader in my index servlet the file wont upload which throws no exceptions, and nor do does it have any compile time errors.

However, If I create a test servlet called fupload, code as follows:



The file will upload as expected with no errors or exceptions of any kind. The problem is why would it work in one servlet (fuploader) and not in another (index servlet)? Thats what I cant get my head around because clearly the FileUploader works but only when it feels like lol. Help much appreciated!
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ive got a rather interesting update. I've changed the index servlet code to this:



As you can see ive commented out the FormController object and now the file WILL upload! Any explanations as to why its doing this? cheers.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway of creating two separate request objects? one for processing the form data and one for processing the file/s??
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I was writing code to process file uploads I recall that I had to take great care to handle the form parameters and the file upload parameters at the same time. Sometimes the relevant form parameters came after file upload parameters which needed to know them, but that wasn't a big problem. But yes, you can only process the parameters once, so your efforts to implement something which processes the parameters twice are going to be in vain.

If you consider that allowing the parameters to be processed twice would require the server to stash away a possibly enormous chunk of code (the upload request includes the entire contents of the file being uploaded) so that it could go through it a second time if the user needed that, you can perhaps see why it doesn't work that way.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:When I was writing code to process file uploads I recall that I had to take great care to handle the form parameters and the file upload parameters at the same time. Sometimes the relevant form parameters came after file upload parameters which needed to know them, but that wasn't a big problem. But yes, you can only process the parameters once, so your efforts to implement something which processes the parameters twice are going to be in vain.

If you consider that allowing the parameters to be processed twice would require the server to stash away a possibly enormous chunk of code (the upload request includes the entire contents of the file being uploaded) so that it could go through it a second time if the user needed that, you can perhaps see why it doesn't work that way.


Maybe I should write a special method which processes both the formdata and fileupload at the same time?, instead of doing it separately like ive been doing. Would you agree to that?
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:Maybe I should write a special method which processes both the formdata and fileupload at the same time?, instead of doing it separately like ive been doing. Would you agree to that?


Absolutely. I'm almost certain that should work for you, although it would require a rewrite of what you've got so far. But fortunately you don't have a whole ton of code to deal with.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Michael Portman wrote:Maybe I should write a special method which processes both the formdata and fileupload at the same time?, instead of doing it separately like ive been doing. Would you agree to that?


Absolutely. I'm almost certain that should work for you, although it would require a rewrite of what you've got so far. But fortunately you don't have a whole ton of code to deal with.


Thanks you've been a great help!! I've nearly completed the code, once ive done it ill test it and ill post it on here and see what you think.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...One more question, when a form is submited via multipart you need to access the data in a special way which I already know how to do. But what GET data? Can I still access that the normal way IE? request.getParameter("some get var") ???
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that you have to use the POST method when you're uploading a file, but I could possibly be wrong about that.

Anyway... I'd suggest writing some simple test code to find out answers to questions like that. Write really, really simple test code, just enough code to test a hypothesis.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It seems to me that you have to use the POST method when you're uploading a file, but I could possibly be wrong about that.

Anyway... I'd suggest writing some simple test code to find out answers to questions like that. Write really, really simple test code, just enough code to test a hypothesis.


Thanks. I wrote up a quick HTML test file containing the following code:



and in my test servlet I wrote:



When I visited the upload.html page I selected a test file and clicked Upload. The servlet printed "edit_profile". So grabbing GET data from the URL works even when the form is a POST multipart. With working it will make my life a whole lot easier lol.
 
Michael Portman
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES!!! It worked! Finally I've got this sorted once and for all
 
Paul Clapham
Sheriff
Posts: 22279
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Portman wrote:So grabbing GET data from the URL works even when the form is a POST multipart. With working it will make my life a whole lot easier lol.


When you say "GET data" I'm guessing you actually mean request parameters. Those can be included in an HTTP request of any type -- GET, POST, and the rest.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!