繁体   English   中英

在Blackberry中上传UTF-8文本文件

[英]Uploading an UTF-8 text file in Blackberry

我正在尝试将UTF-8文本文件上传到Blackberry中的服务器。 上传效果很好,但是当我检查服务器中的文件时,它是一个ASCII文件,而我需要的是UTF-8文件。

这是创建文件时使用的代码:

FileConnection fc = (FileConnection)Connector.open(fileName);
if (!fc.exists()){
    fc.create();
}
long byteOffset = fc.usedSize();
OutputStream outStream = fc.openOutputStream(byteOffset);           
outStream.write(line.getBytes("UTF-8"));
outStream.close();
fc.close();

要发送文件,我使用以下命令:

public void run (){

    httpConnection = null;
    _connectionURL = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--"; 
    String boundary = "*****";  
    int rc = -1;
    OutputStream os = null;

    try {

        _connectionURL = Constants.UPLOAD_URL + getConnectionString();

        httpConnection = (HttpConnection)Connector.open(_connectionURL);
        byte [] postDataBytes = getData();

        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());
        os.write(postDataBytes);
        os.write(lineEnd.getBytes());
        os.write((twoHyphens + boundary + twoHyphens + lineEnd).getBytes());
        os.flush(); 


        // Response
        rc = httpConnection.getResponseCode();
        InputStream in = httpConnection.openInputStream();
        int ch;
        StringBuffer stringBuffer = new StringBuffer();
        while( ( ch = in.read() ) != -1 ){
            stringBuffer.append( (char)ch );
        }            
        String responseString = stringBuffer.toString();

        ...

    }catch (IOException ioe){
       ...
    }
}

...

private byte[] getData() throws IOException {
    int _c;
    StringBuffer _stringBuffer = new StringBuffer("UTF-8");
    FileConnection fileForUpload = (FileConnection) Connector.open(Constants.FOLDER_FILES+this.fileName, Connector.READ);
    this.fileInputStream = fileForUpload.openDataInputStream();
    this.postData = new URLEncodedPostData("UTF-8", false);
    while( (_c = this.fileInputStream.read()) != -1){
        _stringBuffer.append((char)_c);         
    }
    postData.setData(_stringBuffer);
    byte [] _postData = postData.getBytes();
    fileForUpload.close();
    return _postData;
}

我猜getData()方法或httpConnection属性中有问题,但是我不知道这是什么。

谢谢你的帮助

看下面的代码,它出现两次:

while( ( ch = in.read() ) != -1 ){
    stringBuffer.append( (char)ch );
}

在ISO-8859-1中有效地将每个字节视为一个单独的字符。

如果您确实想将内容转换为文本,则应该使用具有UTF-8编码的InputStreamReader ,然后理想地读取字符 (而不是一次读取一个字符)。

这没有帮助:

byte [] _postData = postData.getBytes();

那将使用平台默认编码将字符串转换为字节-几乎从来都不是您想要的。

鉴于您的getData方法尝试将文件读取为字节数组 ,因此IMO根本不应该将其转换为文本。 如果您事先知道文件长度,则应该只创建一个大小合适的字节数组,然后重复调用InputStream.read(byte[], int, int) ,注意返回值以查看已读多远。 如果不这样做,则可以反复读取一个较小的缓冲区,然后将刚刚读取的数据写入ByteArrayOutputStream ,以便稍后从中获取字节数组。

此外,您似乎永远不会关闭任何流-在finally语句中应该这样做,以便即使抛出异常也可以关闭流。

除了乔恩·斯基特(Jon Skeet)的答案。

要从文件读取字节数组,您只需使用net.rim.device.api.io.IOUtilities

FileConnection fileForUpload = 
        (FileConnection) Connector.open(path, Connector.READ);
InputStream stream = fileForUpload.openInputStream();
byte[] data = IOUtilities.streamToBytes(stream);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM