Blackberry jde:如何使用 MultipartPostData 在服务器中上传图像

发布于 2024-10-22 12:19:05 字数 644 浏览 2 评论 0原文

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

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

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);

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

有人可以帮助建立一个发送图像的工作请求吗?

多谢。

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

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 ?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-10-29 12:19:05

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

    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();
}

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

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();
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文