Solved

Upload files via Post using Java

Posted on 2007-11-28
8
1,010 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

756 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