0

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プロパティに何か問題があると思いますが、それが何かわかりません。

ご協力いただきありがとうございます

4

2 に答える 2

2

2 回表示されるこのコードを見てください。

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

これは、実質的に ISO-8859-1 で、各バイトを個別の文字として扱っています。

本当にコンテンツをテキストに変換したい場合はInputStreamReader、UTF-8 のエンコーディングで を使用し、理想的には (一度に 1 文字ではなく) 文字のブロックを読み取る必要があります。

これも役に立ちません:

byte [] _postData = postData.getBytes();

これは、プラットフォームのデフォルトのエンコーディングを使用して文字列をバイトに変換することになります - それはあなたが望むことはほとんどありません.

メソッドがファイルをバイト配列としてgetData読み取ろうとしている場合、それをテキストに変換するべきではありません.IMO . ファイルの長さが事前にわかっている場合は、適切なサイズのバイト配列を作成し、 を繰り返し呼び出して、戻り値に注意して、どこまで読んだかを確認する必要があります。そうでない場合は、小さいバッファーに繰り返し読み取り、読み取ったばかりのデータを後でバイト配列を取得できる に書き込むことができます。InputStream.read(byte[], int, int)ByteArrayOutputStream

さらに、finally例外がスローされた場合でもストリームが閉じられるように、ステートメントでこれを行う必要があります。

于 2013-01-03T15:20:54.757 に答える
2

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);
于 2013-01-03T21:23:28.527 に答える