2

我正在使用此命令将 jpeg 发送到我的网络服务器:

curl -i --data-urlencode "文章附件[内容]@/root/130405-BS007.jpeg" http:///api/create/attach/13697/2/test.jpg/jpeg

并且完美运行...

但在 Android 上,我正在使用以下代码:

public String performPost(File file, String url) {
    HttpPost httppost = new HttpPost(url);
    InputStream content = null;

    try {
        MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        Log.d("DEBUG", "UPLOAD: file length = " + file.length());
        Log.d("DEBUG", "UPLOAD: file exist = " + file.exists());
        ContentBody cbFile = new FileBody(file);
        mpEntity.addPart("articleattachement[content]", cbFile);

        httppost.setEntity(mpEntity);

        Log.d("DEBUG", "UPLOAD: executing request: " + httppost.getRequestLine());
        Log.d("DEBUG", "UPLOAD: request: " + httppost.getEntity().getContentType().toString());

        content = execute(httppost).getEntity().getContent();

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = content.read(buf)) > 0) {
            bout.write(buf, 0, len);
        }
        content.close();
        return bout.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

结果很奇怪......我有一个 jpeg 文件,其中包含以下内容:

[root@hacvm0 13697]# 更多 130405-BS007-1.jpeg

/tmp/phpQ4q9sI

有什么想法可以解决这个问题吗?

如何正确使用文件的 url 编码?

谢谢 !

编辑:

使用此代码完成的新测试:

    public String performPost(File file, String url) {
    HttpPost httppost = new HttpPost(url);
    InputStream content = null;

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("articleattachement[content]", encode(file)));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        Log.d("DEBUG", "UPLOAD: executing request: " + httppost.getRequestLine());
        Log.d("DEBUG", "UPLOAD: request: " + httppost.getEntity().getContentType().toString());

        content = execute(httppost).getEntity().getContent();

        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = content.read(buf)) > 0) {
            bout.write(buf, 0, len);
        }
        content.close();
        return bout.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public String encode(File file) throws IOException {
    byte[] data=null;
    try {

        data = getFileBytes(new FileInputStream(file.getAbsolutePath()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Base64.encodeToString(data,Base64.URL_SAFE);
}

public byte[] getFileBytes(InputStream ios) throws IOException {
    ByteArrayOutputStream ous = null;
    //InputStream ios = null;
    try {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        //ios = new FileInputStream(file);
        int read = 0;
        while ((read = ios.read(buffer)) != -1)
            ous.write(buffer, 0, read);
    } finally {
        try {
            if (ous != null)
                ous.close();
        } catch (IOException e) {
            // swallow, since not that important
        }
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
            // swallow, since not that important
        }
    }
    return ous.toByteArray();
}

jpeg 文件的结果如下:

[root@hacvm0 13697]# 更多 130405-BS007。jpeg _9j_4AAQSkZJRgABAQAAAQABAAD_2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj_2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj_wAARCAJiArwDASIA AhEBAxEB_8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL_8QAtRAAAgEDAwIEAwUFBAQA AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3 ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4-Tl5ufo6erx8vP09fb3-Pn6_8QAHwEA AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL_8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3 uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD6hooo qCAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKzfEetWPh3Q73V9 XmWGxs4zLK_sOw9STgAdyQK5T4XfFTw_8RLN20mVrfUYhmawnI81BnG4dmX3HrzigDvaKKKACiii gAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKA CiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAK KKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKK8l_aP8fTeD fA8kGlSEazqQaGAofmhjx-8mx14BAB7FgaAPD_2ofiZP4i10eHdDnJ0WwlIlkiYEXM_KkHH8K_MA D1yTyNpr2X9nr4bQfD7wq-ra1HHBrt9CHumlIAtYhyI8np0y3v7KK8c_ZN-HP9va-3irVoS2maZI FtlbpNcjBB9wnB_3ivoa9m_ai0jxBqfw2mk8PXTi2tGM9_aoObiEDrnuFxuK9CPoAQD123njuYI5 oJElhkUOjowZWUjIII6gipa-Y_2SPiFPOsvhLW72OVzum00O5MgUZLx9MBRgsASCOcAjp9OUAFFF FABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUU AFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQA UUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAV8E_GbxDdfE3 4wyW-jt58PnppmmqD8rANt3A-jOWbPoR6V9VftD-L38JfDXUXs2ZdSvgbO22feXcDvf2CoGOex2- teE_sdeCzqfim98U3kebXS18m3J6NO45PvtQn8XU0IaPqfwT4dtPCfhbTdDsQPJs4Qm49ZH6u592 Ylj9a2XVXRldQysMEEZBFOooEfAmrqvwr-ONwLaD91pWpLLHuZsiBiHAGCOsbbec5zX3yjK6KyMG VhkEHIIr46_bH0s6d8Q9N1WFE26hYhWLIG3PGxBPIx90pX0n8FtZ_t34V-GL8vvkaySKRvV4_wB2 x_NDQB2lFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFA BRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAF [root@hacvm0 13697]#13697]#13697]#13697]#13697]#

...

4

0 回答 0