post file

Hi all:
      I want to  post a file  to serverlet by java application;I used the java class urlconnection. But I got error from server like this:'premature ending '.Can you tell me why?
 
/**
* Title: testPostFile.java
* Author: Star Lee
* Date: 10/30/2003
* Desc:  Will post a file based to a server-based file upload application
*        Change the filename and the URL for your usage.
*
*/
import java.util.*;
import java.io.*;
import java.net.*;

class testPostFile
{
  // true globals
  // Anything that's unique will work
  static final String CONTENT_BOUNDARY = 
"-----------------------------7d22a63810058e";//Set the boundary

  public testPostFile() {
  }//Structure function

  public static void main (String [] args) {

    // whatever file you care to upload
    String uploadFileName = "c:/document.doc";//Fill the name of file that you want to upload
    String ext = "doc";
    String style = "scalc: HTML (StarCalc)";
    FileInputStream fis1 = null;
    OutputStream os1 = null;
    InputStream is1 = null;
    try
    {
     
      File file1 = new File(uploadFileName);//Create a new file named uploadName.
      fis1 = new FileInputStream(file1);// Fill file into FileInputstream.
      // the URL of your upload application
      URL testPost = new 
      URL("http","192.168.1.90",80,"/servlet/IsItWorking");//Init URL
      URLConnection huc1 = testPost.openConnection();//Open connection

      ((HttpURLConnection)huc1).setRequestMethod( "POST");
      System.err.println("OPEN connection.");
      huc1.setAllowUserInteraction(true);//
      huc1.setDoOutput(true); // turns it into a post
  //    huc1.setUseCaches(false);
  //    huc1.setRequestProperty("Host", "hi/iq");
   //   huc1.setRequestProperty( "Referer", "http://192.168.1.90:80/servlet/ConverterServletLI");
      huc1.setRequestProperty("User-Agent", "IE/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126");
      huc1.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1");
      huc1.setRequestProperty("Accept-Language", "en-us, en;q=0.50");
      huc1.setRequestProperty("Accept-Encoding", "gzip, deflate, compress;");
      huc1.setRequestProperty("Accept-Charset", "ISO-8859-1, utf-8;q=0.66, *;");
      huc1.setRequestProperty("Keep-Alive", "300");
      huc1.setRequestProperty("Connection", "Keep-Alive");
      huc1.setRequestProperty("Content-Type", "multipart/form-data;boundary=" +   CONTENT_BOUNDARY);
      huc1.setRequestProperty("CACHE-CONTROL", "no-cache");
      file://huc1.setRequestProperty("Content-Length","227840" );
      
      
    //  OutputStreamWriter os1 = new OutputStreamWriter(huc1.getOutputStream());
      
          
      os1 = huc1.getOutputStream();
   
      System.err.println("GET OUT STREAM");
   

      // Field 1 a Field of data
    
     os1.write((CONTENT_BOUNDARY + "\r\n" +"Content-Disposition: form-data; name=\"converttype\"\r\n\r\n"+"swriter: MS Word 97','swriter: MS Word 97"+"\r\n").getBytes());
     os1.write((CONTENT_BOUNDARY + "\r\n" +"Content-Disposition: form-data; name=\"extension\"\r\n\r\n"+ext+"\r\n").getBytes());
     
     // os1.write((CONTENT_BOUNDARY + "\r\n" +"Content-Disposition: form-data; name=\"txtTitle\"\r\n\r\\r\n").getBytes());
      // Field 2 - the file, name is "uploadFile"
     os1.write((CONTENT_BOUNDARY + "\r\n" + "Content-Disposition: form-data; name=\"DocumentToBeConverted\"; filename=\"" +uploadFileName+ "\"\r\nContent-Type: application/octet-stream\r\n\r\n").getBytes());

      byte [] fileStuff = new byte[512];
      int howMany = -1;
      int totMany = 0;
      howMany = fis1.read(fileStuff, 0, 512);
      while (howMany != -1) {
        totMany += howMany;
        os1.write(fileStuff, 0, howMany);
        howMany = fis1.read(fileStuff, 0, 512);
      }
      System.err.println("read " + totMany + " bytes from file, wrote to outputstream.");
      fis1.close();
      fis1 = null;
     
      os1.write(("\r\n" + CONTENT_BOUNDARY + "--\r\n").getBytes());
     
      is1 = huc1.getInputStream();
      byte [] urlStuff = new byte[512];
      howMany = is1.read(urlStuff, 0, 512);
      while (howMany != -1) {
        System.out.write(urlStuff, 0, howMany);
        howMany = is1.read(urlStuff, 0, 512);
      }

      System.err.println("that was your output.");
      is1.close();
      is1 = null;
      os1.close();
      os1 = null;
   }
    catch (Exception ex) {
    System.err.println("Exception: " + ex);
    ex.printStackTrace();
    }
    finally {
      if (fis1 != null)
        try {
          fis1.close();
        }
        catch (Exception ok_to_eat) {
          // ok to ignore this
        }
      if (is1 != null)
        try {
          is1.close();
        }
        catch (Exception ok_to_eat) {
          // ok to ignore this
        }
      if (os1 != null)
        try {
          os1.close();
        }
        catch (Exception ok_to_eat) {
          // ok to ignore this
        }
    }
  }
}

Received on Monday, 10 November 2003 03:07:03 UTC