Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with posting multipart file data with httpUrlConnection

807605Aug 31 2007 — edited Sep 1 2007
Hi.

I have a web site (www.doc2pdf.net) that converts .doc files to .pdf.

I recently upgraded it and want to create a java program that allows me to check whether the conversion works, every 15 minutes or so.
I intend to run it by cron via another server.

I'm having problems with uploading the file and getting the pdf file as a response.

This is my code so far:
            URL doc2pdf = new URL("http://69.56.173.114");
             File f = new File("c:\\temp\\test.doc");
            long filesize = f.length();

            FileInputStream fis = new FileInputStream(f);


            HttpURLConnection conn = (HttpURLConnection) doc2pdf.openConnection();
            conn.setRequestMethod("POST");
            conn.setInstanceFollowRedirects(true);
                       conn.setDoOutput(true);
                                conn.setRequestProperty
    ("Content-Type", "multipart/form-data");
               conn.setRequestProperty("Content-Length",""+filesize);
             conn.setRequestProperty("Cache-Control","no-cache");
            conn.setRequestProperty("Content-Disposition","form-data; name=\"inputDocument\"; filename=\"test.doc\"");

                        
            DataOutputStream dos;                         
                                                      
            OutputStream os = conn.getOutputStream();
            dos = new DataOutputStream(os);

            

            int data;
            while ((data = fis.read()) != -1) {
                dos.write(data);
                
            }
            dos.flush();
            os.flush();
            dos.close();
            os.close();
            DataInputStream in = new DataInputStream(conn.getInputStream());
            FileOutputStream fo = new FileOutputStream("c:\\temp\\test.pdf");
            while ((data = in.read()) != -1) {
                fo.write(data);
            }
                        fo.flush();
The "name=inputdocument" parameter is the name of the text box field where you input the path to the .doc file.

In its present form, what is written in test.pdf is simply the html of the web page.

I don't know specifically what the problem is.

Can someone help me with this with some tips?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 29 2007
Added on Aug 31 2007
3 comments
1,454 views