Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 4
Registered: ‎10-14-2008
My Device: Not Specified

JPEG File Upload error

Hi all,

 

I am working trying to upload files to a server via HTTPS connection. But this upload functionality is behaving in a very unpredictable manner. I try to upload a JPEG file this is giving me errors. I get a connection timed out error. If I try to upload another file of same size, it works perfectly fine. This happens with 8100 4.5 simulator. Has anyone else faced a similar problem? Any help would be appreciated.

Thanks in advance. 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: JPEG File Upload error

It would help if you could post code and any relevant data ( server responses etc).

 

New Developer
Posts: 4
Registered: ‎10-14-2008
My Device: Not Specified

Re: JPEG File Upload error

Here is the code snippet:

 

        httpConnection.setRequestMethod(HttpsConnection.POST);

        OutputStream dout = httpConnection.openOutputStream();

        dout.write(postBytes);

        dout.close();

        int ch;

        is = httpConnection.openInputStream();

        while ((ch = is.read()) != -1) {
          bos.write(ch);
        }

 

It writes the postBytes onto the server. And then I read the server response, it simply hangs, nothing happens at all, there is no server response at all.

 

Thanks.

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: JPEG File Upload error

What do you mean nothing happens?

In cases like this, first try to get as much specific affirmative  info as you can- did you catch and display any errors?

What if any response code was there?

Do you hang in the read loop with any data ( you don't have any obvious way to tell from your code until the reading

finishes)? etc.Just make sure that there was no response and no error.  

If possible, try to send your request to a fake or instrumented server. For local testing, usually

a perl script that opens a server socket on 80 and echos the results is fine ( although sometimes buffers don't flush etc).

 

Once you see what you are sending, you can try to hit your real server using wget or something similar.

Try a copy of what your BB sends and try to find a known working case ( often IE will work for this).  

It could just be as simple as setting content-type, I've been using this,  

 

conn.setRequestMethod(HttpConnection.POST);
     if (requestHeaders == null)
     { conn.setRequestProperty("CONTENT-TYPE","application/x-www-form-urlencoded"); }
         conn.setRequestProperty(
             HttpProtocolConstants.HEADER_CONTENT_LENGTH,
              String.valueOf(postData.length));
         debug_loc=10;
         OutputStream out = null;   
     try {
     out = conn.openOutputStream();
    out.write(postData);
    out.flush();
    out.close();

New Developer
Posts: 4
Registered: ‎10-14-2008
My Device: Not Specified

Re: JPEG File Upload error

Thanks for your suggestion. I have already tried changing HTTPS to HTTP so as to monitor the requests that I send. I found that all data reaches there but there is no response from the server. If I save this file with some tool like ms paint, it works fine. What I guess form this is that some additional headers are appended to it through this and only then it works.

But now I do not have any way to check if the file is corrupted or not. Is there any way I can check this?

 

Thanks.
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: JPEG File Upload error

A number of things come up on google, did you check your server logs?

http://www.google.com/search?hl=en&q=apache+%22post+request%22+mime+type+server+response

http://hc.apache.org/httpcomponents-client/primer.html

 

Who or what is attempting to process your post request?

 

BlackBerry Development Advisor
Posts: 15,723
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: JPEG File Upload error

Are you creating the connection it its own thread?

 

Support - Application stops responding when opening a connection
Article Number: DB-00159

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/Support...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: JPEG File Upload error

I thought this was supposed to occur before the connection but OP claims that request was initiated, at least in some cases.

 

However, it is nice to see my warnings about holding locks have been shown to be useful Smiley Happy

There are concurrency mechanisms that avoid these problems and API's which require holding a lock do create

problems. A neurotic fixation on "safety" of one kind or another, in this case safety of lock combined with

safety of user prompt, leads to deadlock. There is never a reason to hold a lock while doing network io in this context.

In the ealier  post in which I pointed to this error, a reasonable UI thread calls to a stop() method in the poster's code would

hang if he was trying to cancel a hung connection which also held a "this" lock in his run method.

Synchronized should normally just be used around predictable assignments, not calls to uncontrolled entities like networks,

operating systems, or users.

 

 

New Developer
Posts: 9
Registered: ‎10-24-2008
My Device: Not Specified

Re: JPEG File Upload error

Hello everyone!!

 

Bbasmi: Can you post your code? Im working in a Photo Uploader for my BB, I have no problem exploring JPG, BMP files in my device, the problem is how to upload the selected file.

It would help me a Lot!!

 

Thanks!

Matias

 

ps: sory for my english

 

New Developer
Posts: 13
Registered: ‎10-25-2008
My Device: Not Specified

Re: JPEG File Upload error

Hi All ,

I am also trying to post file to ASp server.

I am able to post text to server but I am not able to post file to server.

Although I get response from server the response changes as I make any changes in request headers.

I am trying it with 9000 simulator.

 

HttpConnection http = null;
        OutputStream oStrm = null;
        InputStream iStrm = null;   
        boolean ret = false;
   
        String url = "http://192.168.0.102/IPhoneFile/Default.aspx?fileName=logo.jpg";
       
        http = (HttpConnection) Connector.open(url);
        int date = 418;
       
        String boundary ="multipart/form-data; boundary="+ "-----------------------------4664151417711" ;   
     
     
       http.setRequestProperty("Content-Type",boundary );
        http.setRequestMethod(HttpConnection.POST);
       // http.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
       // http.setRequestProperty("Content-Language", "en-US");
       http.setRequestProperty("Connection", "KeepAlive");
        //form header here c#
        String boundary1 = "\r\n--" + boundary + "\r\n";
        byte boundarybytes[] = boundary1.getBytes();
 
            // Get the header for the file upload
            //original
      // String header = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"sampark.txt\"\r\n Content-//Type: application/octet-stream\r\n\r\n";//
       String header = "Content-Disposition: form-data; name=\"file\";filename=\"logo.jpg\"\r\n  Content-Type: application/octet-stream\r\n\r\n";

     //  String header = String.(headerTemplate, "file", file);//written in above line directly
 
            //convert the header to a byte array
       byte headerbytes[] = header.getBytes();
 
            // Add all of the content up.
            //original
            //File f1= null;
           // f1.find
           //long filesize =  f1.getFileSize(1,"file:///SDCard/sampark.txt");
           //original
           ///New
         //  iStrm =getClass().getResourceAsStream("file:///SDCard/logo.jepg");

FileConnection fconnFrom = null;
           int fileSize = 0;
           fconnFrom =(FileConnection) Connector.open("file:///SDCard/logo.jpg");         
           if (fconnFrom.exists())
            {
                fconnFrom.setReadable(true);
           // fconnFrom.setWritable(true);
            fileSize = (int) fconnFrom.fileSize();
            // http.setRequestProperty("Content-Length",String.valueOf(2+ fileSize +headerbytes.length + 2*boundarybytes.length));
             http.setRequestProperty("Content-Length",String.valueOf( fileSize +headerbytes.length + boundarybytes.length));
                   // DataInputStream dataIn = fconnFrom.openDataInputStream();
                   // data = new byte[fileSize];
                   // dataIn.read(data, 0, fileSize);
                   // dataIn.close();
            }
               
           ///
          
     
 
            // Get the output stream
       //Stream requestStream = httpWebRequest.GetRequestStream();
 
    //  FileInputStream fileStream=new FileInputStream(1,"file:///SDCard/sampark.txt");
 
      ////form header here
       oStrm = http.openOutputStream() ;   
     
      // Write the header including the filename.
      oStrm.write(boundarybytes);
       oStrm.write(headerbytes);
      
       //
        byte imgData[] = new byte[fileSize];
        DataInputStream dataIn = fconnFrom.openDataInputStream();      
        int writtenBytes = 0;       
        dataIn.read(imgData, 0, fileSize);
       
        ///added on 10/11/2008
        int index = 0;
        int size = 1024;
        do
        {
           // System.out.println("write:" + index);
            if((index+size) > imgData.length)
            {
                size = imgData.length - index;
            }
            oStrm.write(imgData, index, size);
            index+=size;
        }while(index<imgData.length);       
        ///added on 10/11/2008
       
       
       // oStrm.write(buffer);//original
       
        dataIn.close();

      
    
        
      
       // boundarybytes = boundary1.getBytes();
            // Write out the trailing boundry
        oStrm.write(boundarybytes);
    
       fconnFrom.close();

     
     
       iStrm = http.openInputStream();     
      // Three steps are processed in this method call     
      ret = processServerResponse(http, iStrm);//original
   

}
            catch ( Exception e )
            {
            //  System.out.println("IO error trying to read your input line!");
            //  e.printStackTrace();
            System.out.println(e);
            System.exit(1);
            }
            finally
            {
            // Clean up
            //if (iStrm != null)
                //iStrm.close();
            //if (oStrm != null)
            //  oStrm.close();       
            //if (http != null)
            //  http.close();
            }

 

I get following response for this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
 IPhoneserver
</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx?fileName=logo.jpg" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTsFv8JdRmjvVbcv0xT3d3jn4oUaQ==" />

  <div>   
    </div>
    </form>
</body>
</html>