Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Upload files via Post using Java

Posted on 2007-11-28
8
Medium Priority
?
1,017 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:
Suggested Courses

927 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