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
Developer
Posts: 82
Registered: ‎01-11-2013
My Device: DevAlphadevice
My Carrier: rim
Accepted Solution

Upload audio to webserver

Hi,

 

I am trying to upload audio to webserver using multipart request but it is working good in java server but for php server uploaded data size ,i am getting 0 byte so plz help in sloving this issue. in BB10 multipart working god for same php server , only the problem in java Blackberry devices.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload audio to webserver

We will need more that this unfortunately.  Can you describe exactly what happens on the Blackberry end during this process.  In your debugging, does the send processing work as you would expect it to?  What connection method are you using?  Do you see an exception?

 

Unfortunately there is very little shared between BB10 and BBOS code.  And BB10 will be using the carrier wireless or WiFi only, and it determines this automatically - in BBOS you have to do this sort of thing yourself (or use ConnectionFactory). 

Developer
Posts: 82
Registered: ‎01-11-2013
My Device: DevAlphadevice
My Carrier: rim

Re: Upload audio to webserver

Hi,

 

Thks for reply,

 

When i run my code its working fine without any exceptions,but uploaded data(audio) size is 0 byte  when i checked from server side .but same multipart request code works fine in java server so plz help me sloving this issue.

 

 

try
                {
                    timer.schedule(new TaskTimer(),TIME_OUT);
                    hc = (HttpConnection) Connector.open(url+gatewayURL,Connector.READ_WRITE, true);
                    //hc.setRequestProperty("User-Agent", CacheContent.getCacheContent().getUserAgent());
                    hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
                    hc.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
                    hc.setRequestMethod(HttpConnection.POST);
                    OutputStream dout = hc.openOutputStream();
                    dout.write(postBytes);
                    dout.flush();
                    dout.close();
                    dout=null;
                    postBytes=null;
                    if (hc.getResponseCode() == HttpConnection.HTTP_OK) {

                        int ch;
                        is = hc.openInputStream();
                        while ((ch = is.read()) != -1) {
                            bos.write(ch);
                        }
                        res = bos.toByteArray();
                        System.out.println("http response got>>"+res.toString());
                        String response=new String(res);
                        String res1=response.trim();
                        response=null;
                        System.out.println("http response got1>>"+res1);
                    }

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload audio to webserver

[ Edited ]

"same multipart request code works fine in java server"

Are you sure?

 

I am not an expert in the use of multipart/form-data, but my understanding is that you use this around the data that you are sending, otherwise the Server doesn't find the data.  I don't see you using your 'boundary' as a boundary anywhere. 

Developer
Posts: 82
Registered: ‎01-11-2013
My Device: DevAlphadevice
My Carrier: rim

Re: Upload audio to webserver

 httpMultipartReq = new HttpMultipartRequest("server url" ,"upload_field", "fileName.mp3", "audio/mp3",filedataa); 

httpMultipartReq.send();

 

 

 

package mypackage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

import net.rim.blackberry.api.browser.MultipartPostData;
import net.rim.blackberry.api.browser.PostData;
import net.rim.device.api.io.http.HttpProtocolConstants;

public class HttpMultipartRequest
{
    public static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
    private static String gatewayURL =";deviceside=false;ConnectionType="   "  ";//";

    private static final int CHUNK_SIZE =1024;
    byte[] postBytes = null;

    String url = null;

    byte[] filedataa = null;


    private int TIME_OUT=750000;


    private Timer timer;

    private boolean connectionclose=false;


    HttpConnection hc = null;

    InputStream is = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    public HttpMultipartRequest(byte[] fileBytes) throws Exception
    {
        filedataa=fileBytes;
        
        
    }

    public HttpMultipartRequest(String url,String fileField, String fileName, String fileType,byte[] fileBytes) throws Exception
    {
    
        
        this.url = url;
        filedataa=fileBytes;
        String boundary = getBoundaryString();
        if(fileBytes!=null)
        System.out.println("filebyte not nulllllll");
        String boundaryMessage = getBoundaryMessage(boundary, fileField, fileName, fileType);

        String endBoundary = "\r\n--" + boundary + "--\r\n";

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bos.write(boundaryMessage.getBytes());

        bos.write(fileBytes);

        bos.write(endBoundary.getBytes());

        this.postBytes = bos.toByteArray();

        bos.close();

        bos=null;

        timer=new Timer();
        
        
        
        
        
        
        
        
        
        

    }




    String getBoundaryString()
    {
        return BOUNDARY;
    }





    String getBoundaryMessage(String boundary,String fileField, String fileName, String fileType)
    {
         StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
         res.append("Content-Disposition: form-data; name=\"")
        .append(fileField).append("\"; filename=\"")
        .append(fileName).append("\"\r\n")
        .append("Content-Type: ")
        .append(fileType)
        .append("\r\n\r\n");
        return res.toString();
            
        
    }



    public void send() throws Exception
    {
        new Thread()
        {
            public void run()
            {
                System.out.println("send method called");
                byte[] res = null;

                try
                {
                    timer.schedule(new TaskTimer(),TIME_OUT);
                    hc = (HttpConnection) Connector.open(url+gatewayURL,Connector.READ_WRITE, true);
                    hc.setRequestProperty("User-Agent", CacheContent.getCacheContent().getUserAgent());
                    hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
                    hc.setRequestProperty(HttpProtocolConstants.HEADER_ACCEPT_CHARSET,
                    "ISO-8859-1,utf-8;q=0.7,*;q=0.7");                        
                    hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH ,Integer.toString(postBytes.length));    
                  //  hc.setRequestProperty("Content-Length", String.valueOf(postBytes.length));
                    hc.setRequestMethod(HttpConnection.POST);
                    OutputStream dout = hc.openOutputStream();
                    dout.write(postBytes);
                    dout.flush();
                    dout.close();
                    dout=null;
                    postBytes=null;
                    if (hc.getResponseCode() == HttpConnection.HTTP_OK) {

                        int ch;
                        is = hc.openInputStream();
                        while ((ch = is.read()) != -1) {
                            bos.write(ch);
                        }
                        res = bos.toByteArray();
                        System.out.println("http response got>>"+res.toString());
                        String response=new String(res);
                        String res1=response.trim();
                        response=null;
                        System.out.println("http response got1>>"+res1);
                    } else {
                        res = ("NetworkUnavailable").getBytes();
                        System.out.println("http response got11>>"+res);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    System.out.println("error taggg"+e.getMessage());
                    res=("NetworkUnavailable").getBytes();
                }
                finally
                {
                    try
                    {
                        if(bos != null)
                        {
                            bos.close();
                            bos=null;
                        }
                        if(is != null)
                        {
                            is.close();
                            is=null;
                        }
                        if(hc != null)
                        {
                            hc.close();
                            hc=null;
                        }
                    }
                    catch(Exception e2)
                    {
                        e2.printStackTrace();
                    }
                }
                if(!connectionclose)
                {
                    if(timer!=null)
                    {
                        timer.cancel();
                        timer=null;
                    }
                    connectionclose=true;
                }
            }
        }.start();


    }

 
    class TaskTimer extends TimerTask
    {

        public void run()
        {
            if(timer!=null)
            {
                timer.cancel();
                timer=null;
            }
            if(!connectionclose)
            {
                connectionclose=true;
                System.out.println("not getting network///");
                closeAll();
            }
        }
    }
    public void cancelupload()
    {
        if(!connectionclose)
        {
            if(timer!=null)
            {
                timer.cancel();
                timer=null;
            }
            connectionclose=true;
            closeAll();
        }

    }

}

above code work fine in java server ,only for php server uploaded dat size am getting 0 so plz help me

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload audio to webserver

Just looking, but while I am, two questions.

 

1) You say:

"above code work fine in java server"

but you would not have this in your Java Server:

private static String gatewayURL =";deviceside=false;ConnectionType="   "  ";//";

I presume there are variations between this and what you run elsewhere?

 

2) Does the code work fine from a SImulator.  You can probalbly test this quite easily by simply changing:

private static String gatewayURL =";deviceside=false;ConnectionType="   "  ";//";

to

private static String gatewayURL =";deviceside=true";

Developer
Posts: 82
Registered: ‎01-11-2013
My Device: DevAlphadevice
My Carrier: rim

Re: Upload audio to webserver

Hi,

 

i am using below gateway url to run code in device

gatewayURL =";deviceside=false;ConnectionType="  "  ";

 

 

if i mention conetnt type mds public in this post is not allowed so it is blank

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload audio to webserver

[ Edited ]

I know that is what you are using, but that will not work on the Simulator.  

 

Can you please answer the questions I asked.  Just to repeat, in case you missed the questions:

 

1) What do you mean by Java Server?  And clearly you are not running exactly the same code on there - this connection suffix will only work on a Blackberry device.   

 

2) Can you please change the code as I described and try it on the Simulator? 

Developer
Posts: 82
Registered: ‎01-11-2013
My Device: DevAlphadevice
My Carrier: rim

Re: Upload audio to webserver

thks for ur response,

 

finally i sloved the issue .the error was in reading data from file sytem.