Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Open Source Projects
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830
this week in the
Programmer Certification
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
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Other Open Source Projects
how to get uploaded file by HttpClient on serevr form request
Brajesh Kumar Hayaran
Greenhorn
Posts: 2
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Every Body
i m using httpclient API to upload zip fle on server it is working fine on client side but i need to store this file in a folder on server
i am not able to get on server .
following is the code .
client side :--
package javaapplication5; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.*; /** * * @author bhayaran */ public class FormLoginDemo { static final String LOGON_SITE = "localhost"; static final int LOGON_PORT = 8080; public FormLoginDemo() { super(); } public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); // 'developer.java.sun.com' has cookie compliance problems // Their session cookie's domain attribute is in violation of the RFC2109 // We have to resort to using compatibility cookie policy GetMethod authget = new GetMethod("/xyz/"); client.executeMethod(authget); System.out.println("Login form get: " + authget.getStatusLine().toString()); // release any connection resources used by the method authget.releaseConnection(); // See if we got any cookies Cookie[] initcookies = client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/xyzr/", false); System.out.println("Initial set of cookies:"); if (initcookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < initcookies.length; i++) { System.out.println("- " + initcookies[i].toString()); } } PostMethod authpost = new PostMethod("/xyz/index.do"); // Prepare login parameters NameValuePair action = new NameValuePair("action", "loginFromGUI.do"); NameValuePair url = new NameValuePair("url", "loginFromGUI.do"); NameValuePair userid = new NameValuePair("username", "username"); NameValuePair password = new NameValuePair("password", "password"); NameValuePair project = new NameValuePair("project", "spire"); authpost.setRequestBody( new NameValuePair[] {action, userid, project}); client.executeMethod(authpost); System.out.println("Login form post: " + authpost.getStatusLine().toString()); // release any connection resources used by the method authpost.releaseConnection(); // See if we got any cookies // The only way of telling whether logon succeeded is // by finding a session cookie Cookie[] logoncookies = client.getState().getCookies(LOGON_SITE, LOGON_PORT, "/xyz/", true); System.out.println("Logon cookies:"); if (logoncookies.length == 0) { System.out.println("None"); } else { for (int i = 0; i < logoncookies.length; i++) { System.out.println("- " + logoncookies[i].toString()); } } // Usually a successful form-based login results in a redicrect to // another url int statuscode = authpost.getStatusCode(); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = authpost.getResponseHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) { newuri = "/"; } System.out.println("Redirect target: " + newuri); GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); System.out.println("Redirect: " + redirect.getStatusLine().toString()); // release any connection resources used by the method redirect.releaseConnection(); } else { System.out.println("Invalid redirect"); System.exit(1); } } } }
serevr side code :---
---------------------
DiskFileUpload fu = new DiskFileUpload(); // If file size exceeds, a FileUploadException will be thrown fu.setSizeMax(1000000); Iterator itr=null; try { List fileItems= fu.parseRequest(request); itr = fileItems.iterator(); fileItems = fu.parseRequest(request); } catch (FileUploadException ex) { Logger.getLogger(ExtendedUploadZippedResourcesAction.class.getName()).log(Level.SEVERE, null, ex); } while(itr.hasNext()) { FileItem fi = (FileItem)itr.next(); //Check if not form field so as to only handle the file inputs //else condition handles the submit button input if(!fi.isFormField()) { System.out.println("\nNAME: "+fi.getName()); System.out.println("SIZE: "+fi.getSize()); //System.out.println(fi.getOutputStream().toString()); //File fNew= new File(getRealPath("/"), fi.getName()); // System.out.println(fNew.getAbsolutePath()); // fi.write(fNew); } else { System.out.println("Field ="+fi.getFieldName()); } }
Ulf Dittmer
Rancher
Posts: 43081
77
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
In the future, please
UseCodeTags
when posting code of any length. It's too hard to read without them. I've added them for you.
Take a look at the other methods of the FileItem class. The easiest to use would be
write
, but the various
getXYZ
methods might also be of interest.
Always! Wait. Never. Shut up. Look at this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
unable to login to a website. tried both httpclient and defaulthttpclient
Logging in with HttpClient from Apache
Auto-submit a form on a jsp page
Login and Download file over https
Apache HttpClient posing trouble
More...