0

I'm trying to build a small Java app for connecting to an application called CampFire and am running into trouble trying to upload files to the system. The Java code I'm using to upload a file is as follows:

public static String postFile(String requestUri, File f)
{
  debug("Running postFile.");
  logIn();

  debug("Sending File: " + f.getAbsolutePath() + " to " + campFireURL + requestUri);

  URL url;
  URLConnection conn;

  String linebreak = "\r\n";
  String boundary = "**********xxx**********";
  String twoHyphens = "--";


  String result = "";
  String request = twoHyphens + boundary + linebreak +
    "Content-Disposition: form-data; name=\"upload\"; filename=\"" + f.getName() + "\"" + linebreak +
    linebreak +
    "";
  debug("Request: " + request);

  try
  {
   FileInputStream in = new FileInputStream(f);

   auth.resetTries();
   Authenticator.setDefault(auth);

   // Send data
   url = new URL(campFireURL + requestUri);
   conn = url.openConnection();
   conn.setDoOutput(true);
   conn.setDoInput(true);
   conn.setUseCaches(false);

   conn.setRequestProperty("Connection", "Keep-Alive");
   conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

   DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

   wr.writeBytes(request);

   int i;
   while((i = in.read()) != -1)
   {
    wr.write(i);
   }

   wr.writeBytes(linebreak + twoHyphens + boundary + twoHyphens + linebreak);
   wr.flush();

   wr.close();
   in.close();

   result = readFromConnection(conn);
  }
  catch (Exception e)
  {
   debug(e);
   JOptionPane.showMessageDialog(null, "Error running postData: " + e.getMessage(), "HTTP POST Error", JOptionPane.ERROR_MESSAGE);
   die();
  }

  return(result);
 }

When I run this with a real file though, I get the following errors...

Running postFile.
Sending File: /home/myuser/Desktop/blah.png to https://blah.campfirenow.com/room/blah/uploads.xml
Request: --**********xxx**********
Content-Disposition: form-data; name="upload"; filename="blah.png"
Server returned HTTP response code: 422 for URL: blah blah
java.io.IOException: Server returned HTTP response code: 422 for URL: blah blah

Any idea's what I'm doing wrong here? I'm fairly new at Java and am wondering if maybe I missed something obvious?

Warkior
  • 145
  • 1
  • 3
  • 9

1 Answers1

2

HTTP error 422 means "Unprocessable Entity". After a quick glance I can spot one mistake: a PNG file is a binary file. You've to add Content-Transfer-Encoding: binary to the header of the part.

If it still doesn't work, then you may find the example in the Uploading files section at the bottom of this answer useful.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thanks for the attempt. that particular solution didn't seem to help. You're right though about the binary encoding. I am still unsure how well the two will mix because I'm sending what seems to be bytes of text content followed by a binary file of some sort. (could be anything really) – Warkior Jan 04 '11 at 20:01
  • That's covered by the linked example as well. – BalusC Jan 04 '11 at 20:05
  • Great. Well, I'm making progress. Now getting a 500 error instead of a 422. Thanks for your help though. I think I'm headed in the right direction. – Warkior Jan 04 '11 at 20:31
  • Read the `getErrorStream()` to obtain possible error details. – BalusC Jan 04 '11 at 21:11
  • Which object would have that method? I'm still stuck getting a 500 error. Server returned HTTP response code: 500 for URL: https://xxxxx.campfirenow.com/room/12345/uploads.json java.io.IOException: Server returned HTTP response code: 500 for URL: https://xxxxx.campfirenow.com/room/12345/uploads.json – Warkior Jan 08 '11 at 01:09
  • that is after running: InputStreamReader in = new InputStreamReader(conn.getInputStream()); – Warkior Jan 08 '11 at 01:10
  • Cast `URLConnection` to [`HttpURLConnection`](http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html). For more hints, see also [How to use `URLConnection` to fire and handle HTTP requests](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests) – BalusC Jan 08 '11 at 01:18
  • Unfortunately, that really didn't tell me any more than before. Just that it was getting a 500 error. Is there something I'm missing with the HTTPS piece? I've attempted using HttpsURLConnection but it seemed to make no difference. The strange part is, I set up a test php script on another server to receive uploaded files, and the Java prog works just fine, uploading to that one. The only thing I can think that is different is it's not https. – Warkior Jan 08 '11 at 07:38