Blackberry jde:如何使用MultipartPostData在服务器中上传图像 [英] Blackberry jde : how to upload an image in server using MultipartPostData

查看:131
本文介绍了Blackberry jde:如何使用MultipartPostData在服务器中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我尝试使用MultipartPostData类将图像发送到远程服务器的问题。

Here is the problem I am trying to send an image to a remote server using the class MultipartPostData.

我使用以下代码构建我的PostData:

I build my PostData with the following code :

PostData body = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false);
body.append("deviceID", ""+DeviceInfo.getDeviceId());
body.append("synchro", "true");
body.append("shoot_lat", ""+latitude);
body.append("shoot_long", ""+longitude);
body.append("shoot_place", ""+city);
body.append("shoot_time", String.valueOf(time));
body.append("ref_data", "filename=\"photo.jpg\"");
//image is a byte[]
body.setData(image);

我认为设置图像的方式有问题,因为服务器无法获取此主体(如果我在请求正文中打印服务器收到并分析的数据,我会得到一个空白字符串。)

I think something is wrong in the way of setting the image because the server can not get this body (if I print the data received and analysed by the server in the body of the request, I get a blank string).

任何人都可以帮助建立一个工作请求发送图片?

Can anybody help to build a working request to send an image ?

非常感谢。

推荐答案

好的,我终于找到了自己的答案。

Ok, I finally found the answer by myself.

    private static final String BOUNDARY = "---------------------------14737809831466499882746641449";

public void createHttpMultipartRequest(String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception
{
    this.url = url;
    String boundary = getBoundaryString();
    String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType);

    String endBoundary = "\r\n--" + boundary + "--\r\n";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(boundaryMessage.getBytes());

    if(fileField != null)
    {
        bos.write(fileBytes);
    }
    bos.write(endBoundary.getBytes());

    this.postBytes = bos.toByteArray();
    bos.close();
}

String getBoundaryString()
{
    return BOUNDARY;
}

String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
{
    StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
    String intermediateBoundary = "\r\n--" + boundary + "\r\n";

    Enumeration keys = params.keys();
    while(keys.hasMoreElements())
    {
        String key = (String)keys.nextElement();
        String value = (String)params.get(key);
        res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")    
            .append("\r\n").append(value);

        if(keys.hasMoreElements())
        {
            res.append(intermediateBoundary);
        }
    }

    if(fileField != null)
    {
        res.append(intermediateBoundary);
        res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") 
            .append("Content-Type: ").append(fileType).append("\r\n\r\n");
    }
    return res.toString();
}

Hashtable包含表单的所有参数。 fileBytes包含要发送的数据。

The Hashtable contains all the parameters of the form. And the fileBytes contains the data to send.

这篇关于Blackberry jde:如何使用MultipartPostData在服务器中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆