HttpURLConnection.getInputStream() is returning null

I

igoR Buttler

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?
 
A

ak

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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top