sending a excel file in MultipartEntityBuilder

 private String uploadFile (String filePath, String HTML) throws Exception {

        String postUrl = getUploadUrl(HTML);
        postUrl = "http://printing.**.ca:9191" + postUrl;
        HttpPost post = new HttpPost(postUrl);

        HttpClient client = new DefaultHttpClient();

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        String random = "";
        Random ran = new Random();
        for (int i = 0; i < 28; i++) {
            random = random + String.valueOf(ran.nextInt(9));
        }
        String boundary = "---------------------------" + random; 

        final File file = new File(filePath);

        FileBody fb = new FileBody(file, ContentType.create("application/vnd.ms-excel"), "hello.xls");

        builder.addPart("file[];", fb);
        builder.setBoundary(boundary);

        post.setEntity(builder.build());

        post.setHeader("Host", "printing.**.ca:9191");
        post.setHeader("User-Agent", USER_AGENT);
        post.setHeader("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        post.setHeader("Accept-Language", "en-US,en;q=0.5");
        post.setHeader("Referer",
                "http://printing.**.ca:9191/app");
        post.setHeader("Connection", "keep-alive");
        for (String cookie : this.cookies) {
            for (String c : cookie.split(";")) {
                if (c.contains("JSESSION")) {
                    post.setHeader("Cookie", c);
                }
            }
        }

        HttpResponse response = client.execute(post);
        String reply = sendPost("http://printing.**.ca:9191/app", getUploadParameters(HTML));

        return response.toString();
    }

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source