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 Contributor
creadsmedia
Posts: 3
Registered: ‎01-04-2013
My Device: 9700 Bold
My Carrier: Movistar
Accepted Solution

Sending UTF-8 text file to server

I am trying to upload a UTF-8 text file to a server. The upload works fine but when I check in the server, the file is ASCII.

 

This is the way I create my file:

 

private void writeToFile(String line) throws IOException{ 
		FileConnection fc = null;
		try {
			
			FileConnection fileConnection = (FileConnection)Connector.open(Constants.FOLDER_FILES+fileName);
			if (!fileConnection.exists()){
				fileConnection.create();  
			}
			fileConnection.close();
			
			fc = (FileConnection)Connector.open(Constants.FOLDER_FILES+fileName, Connector.READ_WRITE);
			long byteOffset = fc.usedSize();			
			OutputStream outStream = fc.openOutputStream(byteOffset);			
			outStream.write(line.getBytes("UTF-8"));
			outStream.flush();
			outStream.close();
			

		}catch (IOException ioe){
			UtilsFiles utilsFiles = new UtilsFiles();
			utilsFiles.writeInLog("UtilsFiles error: writeToFile ioexception "+ioe.getMessage());
		} finally {
	        if (fc!=null) {
	            try {
	                fc.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }		     
	}

 

And this the way I upload it:

 

public class HTTPUploader {
		
	private String _connectionURL = null;
	private String fileName;
	public HttpConnection httpConnection = null;
	
	public HTTPUploader(String _fileName) {
		fileName = _fileName;
	}

        public void run (){
		
		httpConnection = null;
		_connectionURL = null;
		String lineEnd = "\r\n";
		String twoHyphens = "--"; 
		String boundary = "*****";	
		int rc = -1;
		OutputStream os = null;

		try	{
			httpConnection = (HttpConnection)Connector.open(_connectionURL);			
			
			httpConnection.setRequestMethod("POST");
			httpConnection.setRequestProperty("Connection", "Keep-Alive"); 
			httpConnection.setRequestProperty("User-Agent", "BlackBerry");
			httpConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");						
			httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, "en-US");
			httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CACHE_CONTROL,"no-cache, no-store, no-transform");			

			os = httpConnection.openOutputStream();
			os.write((twoHyphens + boundary + lineEnd).getBytes());
			os.write(("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName +"\"" + lineEnd).getBytes());
			os.write(lineEnd.getBytes());
			byte [] postDataBytes = getData();
			os.write(postDataBytes);
			os.write(lineEnd.getBytes());
			os.write((twoHyphens + boundary + twoHyphens + lineEnd).getBytes());
			os.flush(); 
			
			rc = httpConnection.getResponseCode();
			InputStream in = httpConnection.openInputStream();
			...
		} 
		catch (ClassCastException cce) 
		{
			...
		} 
		catch (IOException ioe) 
		{
			...
		} 
		finally 
		{
			...
		}
       }

       private byte[] getData() throws IOException	
       {			
                FileConnection fileForUpload = (FileConnection)     Connector.open(Constants.FOLDER_FILES+this.fileName, Connector.READ);
		InputStream is = fileForUpload.openInputStream();
		return IOUtilities.streamToBytes(is);	
       }

...

}

 I receive the file correctly, but it's ASCII and I don't know why.

 

Thanks for your help

 

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sending UTF-8 text file to server

What makes you think the file is ASCII?

 

Remember that ASIII and UTF-8 are not actually different assuming you only have ASCII characters in the file. 

Please use plain text.
New Contributor
creadsmedia
Posts: 3
Registered: ‎01-04-2013
My Device: 9700 Bold
My Carrier: Movistar

Re: Sending UTF-8 text file to server

I use the command: 

 

   file -bi <namefile>

 

And it tells me:

 

  text/plain; charset=us-ascii 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Sending UTF-8 text file to server

So I suspect this is in fact not related to the data you are sending at all.  This is an attribute of the file that the Http Web Server creates for you.  I guess there is some request header that you can set to control that, but I have no idea what that might be.  And it possibly depends on which Web Server you are using. 

 

Sorry the only thing I can suggest is that you have a look at the Web Server documentation?

Please use plain text.
New Contributor
creadsmedia
Posts: 3
Registered: ‎01-04-2013
My Device: 9700 Bold
My Carrier: Movistar

Re: Sending UTF-8 text file to server

You were right when you said: 

 

"Remember that ASIII and UTF-8 are not actually different assuming you only have ASCII characters in the file." 

 

I was testing with a file that contain only ASCII characters, that's why when I used the "file -ib" command I receive " charset: us-ascii"  After that, I tried with special characters within the file and the commands result was "charset: utf-8"

 

Thanks you very much

Please use plain text.