Solved

Upload files via Post using Java

Posted on 2007-11-28
8
1,011 Views
Last Modified: 2013-11-24
I'd like my desktop java application to be able to upload a file to a web form that accepts files.

<form action="multFileUpload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<input name="image1" type="file" id="image1" size="50" /></td>
<input name="image2" type="file" id="image2" size="50" /></td>
</form>

Anyone have any source on how to do this?
0
Comment
Question by:ecuguru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 20371773
Do you want to use existing jars?
http://jakarta.apache.org/commons/fileupload/
0
 
LVL 11

Expert Comment

by:Manish
ID: 20371776
0
 
LVL 92

Expert Comment

by:objects
ID: 20371887
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 92

Expert Comment

by:objects
ID: 20371974
0
 

Author Comment

by:ecuguru
ID: 20371987
Thanks, but..
I'm not looking for a servlet, or a servlet to parse an incoming file request.
I'm looking for the java code that would upload a file to the html script above.

The links above either talk about servlets, or how to receive the file being upoaded.
0
 

Author Comment

by:ecuguru
ID: 20372003
objects:
you're last post helped.
but how do I specify which file I'm uploading?  I'm uploading two specific image files, image1 and image2.  The code snipped only shows a single file.
I would have expected that I need some sort of &image1=Body of image 1?


public class PostAFile {
    private static String url =
         "http://localhost:8080/HttpServerSideApp/GetRequest.jsp";

    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);

        client.setConnectionTimeout(8000);

        // Send any XML file as the body of the POST request
        File f = new File("students.xml");
        System.out.println("File Length = " + f.length());

        postMethod.setRequestBody(new FileInputStream(f));
        postMethod.setRequestHeader("Content-type",
            "text/xml; charset=ISO-8859-1");

        int statusCode1 = client.executeMethod(postMethod);

        System.out.println("statusLine>>>" + postMethod.getStatusLine());
        postMethod.releaseConnection();
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20372053
You need to use a multioart upload if you want to send more than one file at a time. Have a look at the JUpload applet - you can use code from that
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20372061
Use a MultipartPostMethod

        HttpClient client = new HttpClient();
        MultipartPostMethod mPost = new MultipartPostMethod(url);
        client.setConnectionTimeout(8000);

        // Send any XML file as the body of the POST request
        File f1 = new File("students.xml");
        File f2 = new File("academy.xml");
        File f3 = new File("academyRules.xml");

        System.out.println("File1 Length = " + f1.length());
        System.out.println("File2 Length = " + f2.length());
        System.out.println("File3 Length = " + f3.length());

        mPost.addParameter(f1.getName(), f1);
        mPost.addParameter(f2.getName(), f2);
        mPost.addParameter(f3.getName(), f3);

        int statusCode1 = client.executeMethod(mPost);
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 62
program arguments vs VM arguments 4 57
Java array 21 150
Java import explained 4 49
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question