This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes HttpClient file upload code review Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "HttpClient file upload code review" Watch "HttpClient file upload code review" New topic
Author

HttpClient file upload code review

Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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?

Java Programmer | SCJP 1.5 | SCWCD 1.4
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
Yes that's the saddest part..
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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

Joined: Jun 26, 2007
Posts: 136
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

Joined: Jun 26, 2007
Posts: 136
Still I am stuck in this issue.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
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

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

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

Joined: Jun 26, 2007
Posts: 136
The doc states the following info only,

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

Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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

Joined: Jun 26, 2007
Posts: 136
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
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Not really no, have you talked to the ASP people at the other end?
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
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

Joined: Jun 26, 2007
Posts: 136
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpClient file upload code review