0

I'm currently developing a J2ME app. I'm having problems with file uploading. I dont seem to know what part of my code is wrong. Here is my code:

public void UploadImage(long newFileId, String url, String bytes){
    HttpConnection conn = null;
      OutputStream os = null;
      InputStream s = null;
      StringBuffer responseString = new StringBuffer();


      try
      {

         System.out.println(System.getProperty("HTTPClient.dontChunkRequests"));
         conn.setRequestMethod(HttpConnection.POST);
         conn = (HttpConnection)Connector.open(url);
         conn.setRequestProperty("resumableFileId", ""+newFileId);
         conn.setRequestProperty("resumableFirstByte", ""+0);
         conn.setRequestProperty("FilePart", bytes);


         // Read

         s = conn.openInputStream();
         int ch, i = 0, maxSize = 16384;
         while(((ch = s.read())!= -1 ) & (i++ < maxSize)) 
         {
            responseString.append((char) ch);
         }

         conn.close();
         System.out.println(responseString.toString());
         String res = uploadFinishFile(newFileId, bytes);
         if(res.length()>0)
             System.out.println("File uploaded.");
         else
           System.out.println("Upload failed: "+res);
      }
      catch (Exception e)
      {
          System.out.println(e.toString());
      }


}

This is the java code that im trying to convert to j2me:

try {
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(url);
  MultipartEntity me = new MultipartEntity();
  StringBody rfid = new StringBody("" + newFileId);
  StringBody rfb = new StringBody("" + 0);
  InputStreamBody isb = new InputStreamBody(new BufferedInputStream(new FileInputStream(f)), "FilePart");
  me.addPart("resumableFileId", rfid);
  me.addPart("resumableFirstByte", rfb);
  me.addPart("FilePart", isb);

  post.setEntity(me);
  HttpResponse resp = client.execute(post);
  HttpEntity resEnt = resp.getEntity();

  String res = da.uploadFinishFile(login, password, newFileId, DigestUtils.md5Hex(new FileInputStream(f)));
  if(res.isEmpty())
  System.out.println("File uploaded.");
  else
    System.out.println("Upload failed: "+res);
} catch (Exception ex) {
  System.out.println("Upload failed: "+ex.getMessage());
}

1 Answer 1

1

You are uploading the file passing the parameters as HTTP headers, instead of sending the image in the HTTP message body using multipart file upload, compatible with the code you're converting.

Take a look at HTTP Post multipart file upload in Java ME. You can use the HttpMultipartRequest class and change your code to:

Hashtable params = new Hashtable();
params.put("resumableFileId", "" + newFileId);
params.put("resumableFirstByte", "" + 0);

HttpMultipartRequest req = new HttpMultipartRequest(
    url,
    params,
    "FilePart", "original_filename.png", "image/png", isb.getBytes()
);

byte[] response = req.send();
Sign up to request clarification or add additional context in comments.

1 Comment

@JBCagampan The answer was edited to follow your requirements. Please, let me know if it solves your problem and set as accepted answer if so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.