Solved

Upload files via Post using Java

Posted on 2007-11-28
8
1,007 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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