Solved

Upload files via Post using Java

Posted on 2007-11-28
8
1,005 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
 
LVL 92

Expert Comment

by:objects
ID: 20371974
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
pairs challenge 5 44
for i loop in grovy 1 32
Core Java. What output will be and why ? 6 58
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now