将文件上传到Web服务器

时间:2017-10-10 19:47:10

标签: php android http-post multipartentity

如果文件名包含土耳其语字符(ş,ı,ü,ü,ö,ğ),则该文件不会上传到Web服务器。上传到Web服务器是在php页面上完成的。那里没有问题。它上传不包含土耳其字符的文件名(英文字母)。我该如何解决这个问题?

    private String uploadFile() 
    {
        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
        try 
        {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new ProgressListener() {
            @Override
            public void transferred(long num) {
                publishProgress((int) ((num / (float) totalSize) * 100));
            } });

            File kaynakDosya = new File(filePath);

            entity.addPart("dosya", new FileBody(kaynakDosya));
            entity.addPart("islem", new StringBody(GlobalVeri.IslemFotoVideoDosya));

            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(r_entity);
            } else {
                responseString = "Bir hata oluştu! Http Durum Kodu: " + statusCode;
            }           
        } 
        catch (ClientProtocolException e) {
            responseString = e.toString();
        } 
        catch (IOException e) {
            responseString = e.toString();
        }
        return responseString;
    }

1 个答案:

答案 0 :(得分:0)

我相信你缺少UTF-8编码。作为StringBody,将您传递的文本作为带有这些特殊字符的第一个参数,并确保在第二个参数中说出它的utf-8。取代

entity.addPart("islem", new StringBody(GlobalVeri.IslemFotoVideoDosya));

entity.addPart("islem", new StringBody(GlobalVeri.IslemFotoVideoDosya, Charset.forName(HTTP.UTF_8))));