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
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Problem uploading audio to server.

[ Edited ]

Hello everyone, I'm trying to upload an .amr file to the server. My Code as follows :

 

private static void uploadRecording(byte[] data) {
		// TODO Auto-generated method stub
		 byte[] response=null;
		 String currentFile = getFileName();  //the .amr file to upload
		 StringBuffer connectionStr=new StringBuffer("http://www.myserver/bb/upload.php");		 
     	 connectionStr.append(getString());
     	 PostFile req;
		  try {
			req = new PostFile(connectionStr.toString(),
			       			"uploadedfile", currentFile, "audio/AMR", data	);                           		 
			response = req.send(data);			
		  } catch (Exception e) {
				// TODO Auto-generated catch block
			  System.out.println("====Exception: "+e.getMessage());
			} 
  		System.out.println("Server Response : "+new String(response));
}

 

public class PostFile
{
	static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy"; 
	byte[] postBytes = null;
	String url = null;			
	public PostFile(String url, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception
	{	this.url = url; 
		String boundary = getBoundaryString(); 
		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();		
	}
 
	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 byte[] send(byte[] fileBytes) throws Exception
	{
		HttpConnection hc = null; 
		InputStream is = null; 
		ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
		byte[] res = null; 
		try
		{	hc = (HttpConnection) Connector.open(url);				 
			hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());						
			hc.setRequestMethod(HttpConnection.POST);			 
			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(fileBytes.length));						
			OutputStream dout = hc.openOutputStream();			 
			dout.write(postBytes);			 
			dout.close();			 
			int ch;		
			
			 is = hc.openInputStream();			
			if(hc.getResponseCode()== HttpConnection.HTTP_OK)
	        {						
	        	while ((ch = is.read()) != -1)
				{
					bos.write(ch);
				}
				res = bos.toByteArray();
				System.out.println("res loaded..");				
	        }
			else {				
				System.out.println("Unexpected response code: " + hc.getResponseCode());			
				hc.close();
				return null;
			} 	          
		}
		catch(IOException e)
		{				
			System.out.println("====IOException : "+e.getMessage());				
		}
		catch(Exception e1)
			{
				e1.printStackTrace();					
				System.out.println("====Exception : "+e1.getMessage());	
			}
		finally
		{
			try
			{
				if(bos != null)
					bos.close(); 
				if(is != null)
					is.close(); 
				if(hc != null)
					hc.close();
			}
			catch(Exception e2)
			{				
				System.out.println("====Exception : "+e2.getMessage());	
			}
		}
		return res;
	}		
}

 

Highlighted above is the code line which throws the exception: Not Connected.

Can somebody tell me what can be done to correct this? Thanks a lot.

Please use plain text.