Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

HttpClient file upload code review

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
Using help from the various useful discussions posted in this community I successfully used HttpClient API to automate some task. I need to create a java process that will perform login to a secured site then navigate to a particular page which contains one file component to upload a file. Now my java process will pick files from a specified directory and upload all the files my logging into the site instead of logging into the site via browser and uploading files one by one. Below is the code I am using for file upload from my java class
I need some advice regarding the above code like
1. What exactly the line filePost.getParams().setBooleanParameter() doing?
2. As I am uploading multiple files is it ok to create a PostMethod object every time I enter the loop and to release it at the end or I can create one then after all the files uploaded then to release the connection?
3. As I have very limited access to the secured site I have created some webpages of my own and deployed it in Tomcat and it did worked fine but in actual server when I tried to upload a single file using the code after uploading the file name it displays that is uploaded is octet-strea. I am completely clueless about this error. In this type of situation mainly what type of information about the remote server I need to get from the remote server admin?
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) It's setting a parameter that will have some effect when you perform the POST operation, lookup the javadoc to see what HttpMethodParams.USE_EXPECT_CONTINUE does for you

2.) Have you tried both options.....?

3.) So logger.info("File Upload Status for file "+file.getName()+" is "+status+"("+filePost.getStatusLine()+")"); is returning you octect-stream?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I checked the doc for HttpMethodParams.USE_EXPECT_CONTINUE but not understand properly what it saying.
Yes I have tried both the options. If I create a post object once and then loop through and at last release the connection then only one file is being uploaded not all the files. No no after uploading when some one manually login to the site there one option is present to check what are the files that has been uploaded, there in the file name section it shows the name as octect-stream but I am uploading test1.txt file and expecting that name. From my java code I am only getting the status code 200 and from that I conclude that file is uploaded.
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.) There is a nice explanation here on use expect continue.

2.) Great! You answered your own question , just make sure you carefully close you resources each iteration.

3.) Not sure about this one, you need to find out where the server expects to get the file name from. Or are you directly viewing the file via a web browser?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link. I'll check your 3rd point and get back to the discussion. One question I want to clarify , from my java code apart from the status code check can I do any thing else to check whether the file is uploaded or not. The reason of asking this is I don't have the credential to access the site. I need to place some request to some admin they will check and then let me know the result that what is happened actually. This is taking time.

Thank you Martijn .
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you're allowed to view where that file is you can use HTTPClient to check for its existence, it sounds like you're not allow to have access though?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that's the saddest part..
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I discussed from my earlier post that when I am trying to upload a file using my code the file name changed to octet-stream where as the actual file name is test1.txt. Manually when I am logging into the site via browser and uploads the same file it shows the same name test1.txt in the site. I am running my java code from unix box. Is this can be a reason for the name change?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers
From past 2 days I am not able to solve the issue. Any file I am uploading via my java code the site shows the name as octet-stream.
Please help.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still I am stuck in this issue.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I observe few thing regarding this file upload process that I need to discuss, hope that may give some clue to solve the issue. The code what I have posted in my first post, when used shows the uploded file name as "octet-strea". Now I have changed few more line in the code

After that it is showing the file name as "form-dat". So basically it is taking the content type as the file name and first time when I have not given the content type it took the default as text/octet-stream and "octet-strea" as the name. The setName() is not working at all or I am using it in a wrong way? One more thing is that the site is built on asp.net not using jsp or servlet. Can any one give some suggestion.
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so it sounds like you're sending the right file name now, is it just the file that's not arriving or?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you upload a file there is a link to check uploaded file list. In that list it shows file name,file size,user etc. There it shows the name as form-dat.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add that I checked the code locally by creating some jsp files trying to simulate the application and there every thing works fine. Is it due to the asp.net issue?
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's possible yes, the receiving web service could be reading information from the wrong parts of the stream, I'd speak to them to find out
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Martijn.

the receiving web service could be reading information from the wrong parts of the stream

So is it the HttpClient component what we are using doing something wrong or the asp.net pages that receiving my request messing it up?
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well since you've tested HTTPClient against your own 'server' you know it works right? Another trick is to use a Firefox plugin to analyse the request going out (you can inspect the header etc.)
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I have tried that too. The request header what I am getting from browser as follows,


and the response header


And from my code the request header is
Response
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the Javadoc for filePart.setName(file.getName()); say? How does this translate when you send that info to the server? How is the server expected to get that value out?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The doc states the following info only,

public void setName(String name)
Sets the part name.
Parameters:
name -

 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have changed the setContentType to

filePart.setContentType(file.getName());

, I know that is not right but now I am getting the file name properly displayed. Only thing is that it displays only the first 8 character of whatever I give in the content type. So my file name is test1.txt and it displays test1.tx . But manually uploading via the site giving full name of the file.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the following piece of code,


I am getting the following log when log level set to DEBUG. It seems that the file test1.text reaches the server.

File name at the site now again I am getting form-dat. Any idea Martijn?
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really no, have you talked to the ASP people at the other end?
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure whether I can reach them. I'll try. But i am totally lost why the content type it is taking as file name. That too only 8 char , "form-dat".
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's really frustrating time for me. To summarize the problem again for some help. I am using HttpClient to upload files to a site (build using asp)
but once I upload the file the file name what it displays in the next webpage is not right. It follows a pattern,
If I don't use any content type the name it shows is octet-strea, when I specify content type as multipart/form-data it shows form-dat and if I specify text/plain (the right one to be used here) it shows plai .
That means it takes second half of the content-type and removes the last character. I have tried the two different version of code and both gives the same result

The above code is deprecated. And the following code too I checked

Hard luck.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic