HttpURLConnection.getInputStream() is returning null

Discussion in 'Java' started by igoR Buttler, Jan 6, 2004.

  1. igoR Buttler

    igoR Buttler Guest

    OK, got a big problem here, I've spent all night on it and am hoping some
    good soul will be able to help me. I'm trying to post some multipart-data to
    an oracle gateway . It's basically a simple post that can be done from a
    browser in a few lines
    ex:
    <FORM enctype="multipart/form-data"
    action="http://some.server/pls/backup/FILE_UPLOAD.linkFileToLogin"
    method="POST">
    <p>userId:<INPUT type="text" name="userId"><br>
    <p>password:<INPUT type="text" name="pass"><br>
    <p>application:<INPUT type="text" name="application"><br>
    <p>application_version:<INPUT type="text" name="application_version"><br>
    <p>fileType:<INPUT type="text" name="fileType"><br>
    <p>File to upload:<INPUT type="file" name="file"><br>
    <p><INPUT type="submit">
    </FORM>

    This works fine.

    My problem is from my java application, whatever I try, the
    HttpURLconnection keeps throwing a FileNotFoundException when I call
    getInputStream. Nothing seems to work. Stepping through the code, the
    inputSteam is null. Here are the main lines of my code:


    URL pisces = new URL(url);
    urlConnection = (HttpURLConnection)pisces.openConnection();
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setRequestMethod("POST");
    urlConnection.setRequestProperty("Content-Type", "multipart/form-data");

    try
    {
    OutputStream os = urlConnection.getOutputStream();
    out = new DataOutputStream(os);
    String content =
    "userId=" + URLEncoder.encode("blah1")
    + "&pass=" + URLEncoder.encode("blah2")
    + "&application=" + URLEncoder.encode("blah3")
    + "&application_version=" + URLEncoder.encode("blah4")
    + "&fileType=" + URLEncoder.encode("blah5")
    + "&file=" + URLEncoder.encode(jarFile.getAbsolutePath());

    out.writeBytes(content);
    FileInputStream fileInputStream = new FileInputStream(jarFile);
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    while (bytesRead > 0)
    {
    out.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }
    out.flush();

    This is the line that throws the excption
    in = new DataInputStream(urlConnection.getInputStream());
    String str;
    while ((str = in.readLine()) != null)

    etc...

    Where am I going wrong?
     
    igoR Buttler, Jan 6, 2004
    #1
    1. Advertising

  2. igoR Buttler

    ak Guest

    > urlConnection = (HttpURLConnection)pisces.openConnection();
    > urlConnection.setDoInput(true);
    > urlConnection.setDoOutput(true);
    > urlConnection.setUseCaches(false);
    > urlConnection.setRequestMethod("POST");
    > urlConnection.setRequestProperty("Content-Type", "multipart/form-data");


    hmm, I think you forgot
    urlConnection.setAllowUserInteraction(true);


    --

    ____________

    http://reader.imagero.com the best java image reader.
     
    ak, Jan 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joana
    Replies:
    5
    Views:
    5,476
    John C. Bollinger
    Sep 16, 2003
  2. Syed Ali
    Replies:
    1
    Views:
    838
    kpl_pl
    Jul 20, 2007
  3. Replies:
    5
    Views:
    26,709
    Mike Schilling
    Mar 29, 2006
  4. jenn
    Replies:
    0
    Views:
    1,697
  5. jason
    Replies:
    13
    Views:
    883
    John B. Matthews
    May 14, 2010
Loading...

Share This Page